| | |
Generating unique filenames
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
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
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
Michelle (Junior Developer)
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
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
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
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
Michelle (Junior Developer)
![]() |
Similar Threads
- I need advice with my HijackThis log (Viruses, Spyware and other Nasties)
Other Threads in the VB.NET Forum
- Previous Thread: Outputting In Listbox....
- Next Thread: displaying current year in textbox
| Thread Tools | Search this Thread |
.net 2005 2008 access account arithmetic array arrays basic bing button buttons c# center check checkbox code combobox component convert crystalreport data database datagrid datagridview date dissertation dissertations dropdownlist excel fade file-dialog folder ftp generatetags google gridview hardcopy images inline input insert intel internet listview mobile monitor ms net networking objects output passingparameters peertopeervideostreaming picturebox picturebox1 port print printing problem problemwithinstallation project remove save searchbox searchvb.net select serial server shutdown soap sorting survey table tcp temperature text textbox timer toolbox trim update updown user validation vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web winforms wpf





