Vb.net 如何在VisualStudio中使文本文件保存在程序目录中

Vb.net 如何在VisualStudio中使文本文件保存在程序目录中,vb.net,Vb.net,由于我已经放弃了数组方法来解决这个问题,我需要知道如何使listbox始终保存在程序目录中的textfile中,以便可以使用/访问它来填充不同的listbox,有什么想法吗?下面是我的代码 SaveFileDialog1.Filter = "Text files (.txt)|.txt" SaveFileDialog1.ShowDialog() If SaveFileDialog1.FileName <> "" Then Using SW As New IO.Strea

由于我已经放弃了数组方法来解决这个问题,我需要知道如何使listbox始终保存在程序目录中的textfile中,以便可以使用/访问它来填充不同的listbox,有什么想法吗?下面是我的代码

SaveFileDialog1.Filter = "Text files (.txt)|.txt" 
SaveFileDialog1.ShowDialog() 
If SaveFileDialog1.FileName <> "" Then 
    Using SW As New IO.StreamWriter(SaveFileDialog1.FileName, False) 
        For Each itm As String In Me.ListBox1.Items 
            SW.WriteLine(itm) 
        Next 
    End Using 
End If
SaveFileDialog1.Filter=“文本文件(.txt)|.txt”
SaveFileDialog1.ShowDialog()
如果是SaveFileDialog1.FileName“”,则
将SW用作新IO.StreamWriter(SaveFileDialog1.FileName,False)
对于Me.ListBox1.Items中的每个itm作为字符串
西南写入线(itm)
下一个
终端使用
如果结束

对您进行一点研究将有助于您更好地了解您想要实现的目标

  • 如何获取程序数据目录
    My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData
  • 如何将多行写入文件<代码>文件.writeAllines()
  • 如何从文件中读取多行<代码>文件.ReadAllLines()
  • 一旦你了解了基本知识,你就可以很容易地把它们放在一起

    在WinForm上创建两个列表框和一个按钮:

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.
    
        'Get the Program Data Directory (This is hidden by default by the OS.)
        Dim strPath As String = My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData
        Dim fileName As String = "myFile.txt"
        Dim fullPath = Path.Combine(strPath, fileName)
    
        Dim data As String() = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}
    
        'Save the items to ListBox1 First
        For Each item As String In data
            ListBox1.Items.Add(item)
        Next
    
        'Now write the items to the textfile, line by line.
        File.WriteAllLines(fullPath, data)
    
        'Read all lines we just saved and load them onto an array of strings.
        Dim tempAllLines() As String = File.ReadAllLines(fullPath)
    
        'Display each  on ListBox2 by iterating the array.
        For Each line As String In tempAllLines
            ListBox2.Items.Add(line)
        Next
    End Sub
    
    在这里,我创建了这个表单,所以你可以了解我指的是什么


    您可以获得当前可执行文件文件夹的路径,如下所示:

    folderPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
    
    folderPath = Path.GetDirectoryName(Environment.GetCommandLineArgs()(0))
    
    filePath = Path.Combine(folderPath, fileName)
    
    但是,只有当可执行文件是.NET程序集时,这才有效。否则,您可以使用命令行中的第一个参数(即完整的可执行文件路径),如下所示:

    folderPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
    
    folderPath = Path.GetDirectoryName(Environment.GetCommandLineArgs()(0))
    
    filePath = Path.Combine(folderPath, fileName)
    
    另一方面,如果要获取当前程序集的路径(可能与加载该程序集的可执行文件不同),可以执行以下操作:

    folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
    
    或者,如果您只想获取当前目录,可以使用:

    folderPath = Directory.GetCurrentDirectory()
    
    获得文件夹路径后,可以使用
    path将文件名添加到其中。组合
    ,如下所示:

    folderPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
    
    folderPath = Path.GetDirectoryName(Environment.GetCommandLineArgs()(0))
    
    filePath = Path.Combine(folderPath, fileName)
    
    但是,不建议直接将数据写入程序的运行路径,因为用户可能没有写入该文件夹的权限。使用program data文件夹肯定会更好,但即使这样也有风险:

    folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyAppName")
    

    建议将.NET应用程序中的数据存储在以下位置。

    您不能循环浏览列表框中的项目并将其附加到文本文件中吗?向我们展示您迄今为止所做的尝试。我知道如何使用savedialog将它们保存到文本文件中,但将它们保存到程序用户想要的任何目录中我希望它自动保存到程序目录中,这样我就可以使用文本文件填充其他文件listbox@user3459587当你提到
    程序目录
    时,你指的是应用程序域或程序文件或程序数据?我认为程序文件或程序数据,无论程序在哪里存储它需要运行的数据,下面是我的代码`SaveFileDialog1.Filter=“Text Files(.txt)|.txt”SaveFileDialog1.ShowDialog(),如果SaveFileDialog1.FileName“”然后使用SW作为每个itm的新IO.StreamWriter(SaveFileDialog1.FileName,False)作为Me.ListBox1.Items SW.WriteLine(itm)中的字符串,然后结束使用End如果`@user3459587从注释中删除代码,请用您的代码更新问题。