Search code examples
vb.netfilevisual-studio-2013streamreader

How to Access a txt file in a Folder created inside a VB project


I'm creating a VB project for Quiz App (in VS 2013). So I have some preset questions which are inside the project (I have created a folder inside my project and added a text file).

My question is how can I read and write contents to that file? Or if not is there any way to copy that txt file to Documents/MyAppname when installing the app so that I can edit it from that location?


Solution

  • In the example below I am focusing on accessing files one folder under the executable folder, not in another folder else wheres. Files are read if they exists and then depending on the first character on each line upper or lower case the line then save data back to the same file. Of course there are many ways to work with files, this is but one.

    The following, created in the project folder in Solution Explorer a folder named Files, add to text files, textfile1.txt and textfile2.txt. Place several non empty lines in each with each line starting with a character. Each textfile, set in properties under solution explorer Copy to Output Directory to "Copy if newer".

    Hopefully this is in tune with what you want. It may or may not work as expected via ClickOnce as I don't use ClickOnce to validate this.

    In a form, one button with the following code.

    Public Class Form1
        Private TextFilePath As String =
            IO.Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory, "Files")
        Private TextFiles As New List(Of String) From
            {
                "TextFile1.txt",
                "TextFile2.txt",
                "TextFile3.txt"
            }
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim FileName As String = ""
            ' loop thru each file
            For Each fileBaseName As String In TextFiles
                FileName = IO.Path.Combine(TextFilePath, fileBaseName)
                ' only access file if it exist currently
                If IO.File.Exists(FileName) Then
                    ' read file into string array
                    Dim contents As String() = IO.File.ReadAllLines(FileName)
                    ' upper or lower case line based on first char.
                    ' this means you can flip flop on each click on the button
                    For x As Integer = 0 To contents.Count - 1
                        If Char.IsUpper(CChar(contents(x))) Then
                            contents(x) = contents(x).ToLower
                        Else
                            contents(x) = contents(x).ToUpper
                        End If
                    Next
                    ' save changes, being pesstimistic so we use a try-catch
                    Try
                        IO.File.WriteAllLines(FileName, contents)
                    Catch ex As Exception
                        Console.WriteLine("Attempted to save {0} failed. Error: {1}",
                                          FileName,
                                          ex.Message)
                    End Try
    
                Else
                    Console.WriteLine("Does not exists {0}", FileName)
                End If
            Next
        End Sub
    End Class