Search code examples
xmlvisual-studio-2008c#-4.0incrementedi

How do I increment the number in a file I create each time a new file is create?


I am creating a file conversion console app to change xml files generated by an shopping cart export to x12 850 EDI txt files using Visual C#. I need one number in the txt file being created to increment by one, then store this new number in the app itself so that the next file converted increments one past the last file created, and for this to continue with each additional file converted. What is the past way to accomplish this? Thank you.

Tyrel


Solution

  • Well it depends on a lot of things.

    The main issue is going to be one of efficiency.

    The file system watcher MSDN HERE will get you the events of the files being created, but then you will have to deal with all of the potential scenarios, like someone putting in files another type in the directory, multiple files at once, etc.

    If I were designing such a thing I would consider a multithreaded app and some queueing.

    Part#2...

    It will not modify the integer, it will notify you when the files are created / deleted etc. Keeping count of those events will allow you to keep track of the files themselves. Persisting the setting would require something external to the running application such as a registry key or disk file. By queuing events from the file system watcher, you can control access to that persisted file in a clean organized manner ensuring that it always corresponds to the operation at hand, not just a file creation or deletion event.

    Essentially every time the app loads get the last value from the file, as an operation process increment / decrement the value that is in memory and write it back out to that file. The value would be an integer in memory and simply incremented or decremented with a ++ or – as detailed here