[英]vb.net read and write to text file inside resources
我试图列出可以像购物清单一样放入其中的物品清单,但是我的程序无法访问C:\\驱动器,所以我决定将其放入资源文件夹,但是那没用,有人知道如何让它起作用。 ?
我的代码:
Private Sub ThirteenButton14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThirteenButton14.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
Dim i As Integer
w = New IO.StreamWriter(My.Resources.FavoriteList)
For i = 0 To ListBox1.Items.Count - 1
w.WriteLine(ListBox1.Items.Item(i))
Next
w.Close()
ListBox1.Items.Clear()
R = New IO.StreamReader(My.Resources.FavoriteList)
While (R.Peek() > -1)
ListBox1.Items.Add(R.ReadLine)
End While
R.Close()
End Sub
Private Sub ThirteenButton13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThirteenButton13.Click
ListBox1.Items.Add(ThirteenTextBox6.Text + " | " + ThirteenTextBox7.Text)
ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
Dim i As Integer
w = New IO.StreamWriter(My.Resources.FavoriteList)
For i = 0 To ListBox1.Items.Count - 1
w.WriteLine(ListBox1.Items.Item(i))
Next
w.Close()
ListBox1.Items.Clear()
R = New IO.StreamReader(My.Resources.FavoriteList)
While (R.Peek() > -1)
ListBox1.Items.Add(R.ReadLine)
End While
R.Close()
End Sub
这对我没有用。 任何帮助都是y =感谢
应用程序没有“资源文件夹”。 您的项目有一个这样的文件夹,因为这是存储源项目的地方,但是资源的全部目的是将它们编译为EXE本身的数据。 因此,它们是只读的。 如果希望所有用户都可以安全写入的公共位置,请使用公共文档文件夹。
至于您的代码,如果将文件添加到资源中,则该文件的内容将被编译到EXE中并通过My.Resources返回。 在运行时没有文件,因此在运行时没有文件路径,因此您创建StreamReader和StreamWriter的尝试必然会失败。 我假设您已添加文本文件作为资源。 在这种情况下,My.Resources.FavoriteList将以字符串形式返回该文件的内容。 您可以使用该数据,但不能修改它并将更改保存回该资源,因为该资源是EXE的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.