Hey Guys, im a fairly novice programmer

I would just like to hear some views / opinions on how you would go about implementing the following:

I need my program to save data to a textfile giving the file a name and adding a number to the end eg filename_1.txt. So, each time my program runs it saves the data entered to a new textfile (incrementing the number each time)

The way I was thinking of implementing this was to :

1. Create a textfile named count.txt that just contains a number (the number of files created)

2. Each time a new file needs to be created, check the count.txt file to see what the number is, then increment it by one and append it to the new textfile name, then overwrite the number that was in count.txt

3. close the program when data has been entered (creating unique filename)

I have come up with this approach beacuse I need the program to be able to keep track of the number as the user will close the application and only load it when needed.


To me it seems to be a sensible approach, but there are more than likely more efficient ways of doing this. Your comments would be much appreciated

Kind Regards

Michelle

Well if you only needed a unique name then you could just use:

filename = my.Computer.FileSystem.GetTempFileName

If you want something more predictable you can always do this:

    Dim i As Integer = 0
    Dim FileMask As String = "MyFile_{0}.txt"
    Dim Filename As String = ""

    Filename = String.Format(FileMask, i)
    Do While My.Computer.FileSystem.FileExists(Filename)
        i += 1
        Filename = String.Format(FileMask, i)
    Loop

    ' When loop exits, you should have a unique name

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Hi Guys...

Ive had a brainwave .. or at least want to try a different approach.

I want to keep a count of all new files created ... possibly in the mobile devices registry ... or by XML file ....

so my application will boot up for the first time ... realise the count is 0 ... create a new file but increment the count by 1 and attach the number 1 to the new file name ...

Therefore, the next time my application runs, it will check the number in the registry/XML file and increment it by one ---- that way the files created will constantly increment by one each time ... making filenames unque

:) Please could someone help point me in the right direction to do this....

Many Thanks

Elmo

This article has been dead for over six months. Start a new discussion instead.