如何访问 VB 项目中创建的文件夹中的 txt 文件

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

我正在为 Quiz App(在 VS 2013 中)创建一个 VB 项目。所以我在项目中有一些预设问题(我在我的项目中创建了一个文件夹并添加了一个文本文件)。

我的问题是如何读取和写入该文件的内容?或者,如果没有,在安装应用程序时是否可以将该 txt 文件复制到 Documents/MyAppname,以便我可以从该位置编辑它?

这可能对你有帮助

Dim objStreamReader As StreamReader
    Dim strLine As String

    'Pass the file path and the file name to the StreamReader constructor.
    objStreamReader = New StreamReader("C:\Boot.ini")

    'Read the first line of text.
    strLine = objStreamReader.ReadLine

    'Continue to read until you reach the end of the file.
    Do While Not strLine Is Nothing

      'Write the line to the Console window.
      Console.WriteLine(strLine)

      'Read the next line.
      strLine = objStreamReader.ReadLine
    Loop

    'Close the file.
    objStreamReader.Close()

    Console.ReadLine()

您还可以查看 this link。

在下面的示例中,我专注于访问可执行文件夹下一个文件夹中的文件,而不是其他文件夹中的文件。如果文件存在,则读取文件,然后根据每行的第一个字符大写或小写,然后将数据保存回同一个文件。当然,处理文件的方法有很多种,这只是其中一种。

下面,在Solution Explorer中的项目文件夹中创建一个名为Files的文件夹,添加到文本文件中,textfile1.txt和textfile2.txt。在每行中放置几个​​非空行,每行以一个字符开头。每个文本文件,在解决方案资源管理器下的属性中设置复制到输出目录到"Copy if newer"。

希望这符合您的要求。它可能会或可能不会通过 ClickOnce 按预期工作,因为我不使用 ClickOnce 来验证这一点。

在一个表单中,一个带有以下代码的按钮。

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