Is anything wrong with this code? I ask because in the past, I included the text file with the installer. In an attempt to simplify the installer, I decided to make the app generate the text file and put it where it needs to go.

Here's the code (simplified)

First it creates the directory folder-

 Sub CreateCriticalFolder()
        Try
            If (Directory.Exists(x.myCriticalFolder)) Then
                'MsgBox("Critical Folder Already Exists ")
            Else
                Directory.CreateDirectory(x.myCriticalFolder)
            End If
        Catch E As Exception
            MsgBox("Error creating directory")
            MsgBox("Error: {0}", E.Message)
        End Try
    End Sub

Then it creates the text file

Public Shared Sub CreateLocationsAndCallTypes()
        Dim path As String = myCriticalFolder & "Locations and Call Types.txt"
        If File.Exists(path) = True Then
            Exit Sub
        ElseIf File.Exists(path) = False Then
            ' Create a file to write to. 
            Dim sw As StreamWriter = File.CreateText(path)
            sw.WriteLine("Sample Line of text #1")
            sw.WriteLine("Sample Line of text #2")
            sw.WriteLine("Sample Line of text #3")
            sw.WriteLine("Sample Line of text #4")
            sw.Close()
        End If
    End Sub

FWIW, the myCriticalFolder is located here- Environment.SpecialFolder.LocalApplicationData

Users are reporting their "Locations and Call Types.txt" getting wiped out on updates. Sone users are reporting they are unable to edit this file through the app, like it's locked or something.

The code seems to make sense. Is there anything strange with it? Look normal?

Directory.CreateDirectory(x.myCriticalFolder) -> ok so no final "\" on myCriticalFolder. But then,

Dim path As String = myCriticalFolder & "Locations and Call Types.txt"

Missing the "\" between folder and filename

You could use: IO.Path.Combine(x.myCriticalFolder, "Locations and Call Types.txt") instead.

Hi
This wouldn't call an error but I was wondering why you did this:

If File.Exists(path) = True Then
    Exit Sub
ElseIf File.Exists(path) = False Then
....
End if

Surely if File.Exists is not true then it is false... i.e. why not just "Else" I'm just wondering would the way you've done it not them make another call out to the system.IO to find out if the file was there when you've already established that it isn't?

Not getting at you just curious.

TnTinMN, thanks for the reply. I'll check my code and insert the missing \

G_Waddell- No real reason why I did it that way. In my head thats how it made sense. Like "If it's there, then stop- end of story" kind of thing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.