This is more of a design/user experience kind of question, so if it belongs somewhere else please let me know.I have a program that has multiple PictureBoxes:PictureBox1PictureBox2..PictureBox60Each PictureBox's Tag field is filled in with a String value.When the user clicks on a picture box, the Tag property is put inside a ListBox, and I manually order them:1. ListBox item 12. ListBox item 23. ListBox item 3etcEach line is also written to a text file, again with the line number preceeding the item. Condition 1: If there are already items in the file, it detects how many lines are currently in the file and continues the numbering, so the file begins with 1. Example:File1.txt already contains1. Bob2. Sally3. Joeand the ListBox is empty. If a PictureBox with the Tag Charles is clicked, the ListBox now contains:1. Charlesand File1.txt now contains1. Bob2. Sally3. Joe4. CharlesCondition 2: The file does not yet exist, and therefore when items are added to the ListBox and written to the file, the file is created and they are listed in the file the same way as above. The file items are numbered beginning with 1.Condition 3: The file exists, but is empty. I want, regardless of the existence of the file, the numbering in said file to begin with 1 and continue the numbering.Here is the code I have to handle these three conditions. It just feels so messy and unprofessional, but maybe I just need the approval of others to make it feel less so.Private Sub saveToFile(boss As String, item As String)    ' lineCount used so we know where to append in the file i.e. if there were already 5 lines we know to append at the 6th line    ' + 1 to use 1 indexing    Dim lineCount As Integer    Dim fileExists As Boolean = False    ' vars to retrieve item from ItemDropListBox and extract only the item name    Dim lastItem As String = ItemDropListBox.Items.Item(ItemDropListBox.Items.Count - 1)    Dim periodIndex As Integer = lastItem.IndexOf(.) + 2    Dim itemStringLength As Integer = lastItem.Length - periodIndex    Dim trimmedItem As String = lastItem.Substring(periodIndex, itemStringLength)    Dim sw As IO.StreamWriter    ' TODO just me being nitpicky    '   [x] - If file doesn't exist, create it and being first item at 1    '   [x] - If file does exist AND there is already items in it, write to it with correct order    '   [x] - If file does exist and there is NOT items in it, write to it with correct order    ' If the file exists, want to    If My.Computer.FileSystem.FileExists(My.Application.Info.DirectoryPath & /logs/ & boss & .txt) Then        lineCount = IO.File.ReadAllLines(My.Application.Info.DirectoryPath & /logs/ & boss & .txt).Count        fileExists = True    Else        lineCount = 1        fileExists = False    End If    ' True indicates append to file instead of overwrite    sw = New IO.StreamWriter(My.Application.Info.DirectoryPath & /logs/ & boss & .txt, True)    If fileExists = True Then        ' Write the new item to the file        sw.WriteLine((lineCount + 1) & .  & trimmedItem)    Else        sw.WriteLine(lineCount & .  & trimmedItem)    End If    ' Close file    sw.Close()End SubQuestion: Am I trying too hard to fix a problem that will never happen? The files will 99.9% of the time never be created by the user, only by the code, so the chance of them even having a file exist with nothing inside is already very slim. I could have much more elegant code if I didn't worry about handling this small chance.I have also already considered moving to C#. The original idea was to create it in VB then re-create in C#, but I think that would be a waste of time and I have enough experience that I think C# would do just fine. Plus from what I can tell I would have much more control over things I want to change.Edit: Moving all the code over to C# - there's not that much and I feel too limited with VB (not even regarding this question)
Visual Basic Writing to File with Numbered Lines
c#;vb.net;file handling;visual basic
The key is to subdivide your problem into smaller tasks first:separate input, processing and output into three functionsthe input function should read the file and return a list of strings. If the file does not exist, it should return an empty list of strings (and not more!). Note there is no need to remember the information if the file existed in the first place for the further processing. There is also no reason to distinguish the cases of an empty file or a non-existing file.the processing function should get a list of strings as input, the string to be added, and it should return the new list of strings. Note that dealing with the empty list as input should not be really different from dealing with non-empty liststhe output function should take the list of strings and write it back into the file. Note you do not have to tell the StringWriter explicitly that it should create a new file if it does not exist, it will do it automatically for you if you use it in overwrite mode instead of append mode (so in your code, it should be instantiated like New IO.StreamWriter(filename, False).As you see, dealing with empty files or non-existing vs. existing files is only a minor issue here, so making your program robust enough to handle these cases is nothing which should cause you a headache.A final note for porting the VB code to C#: maybe you know C# better than VB (like me), but VB.NET and C# have almost the same capabilities. For learning how to subdivide a bigger problem into smaller tasks, it is irrelevant which of the two languages you use. 