我试图使用www.example.com winforms中的打开文件对话框覆盖保存在外部驱动器上的文本文件,我一直得到错误:vb.net winforms, and i keep getting the error: System.IO.IOException: 'The process cannot access the file 'F:\SETTINGS.TXT' because it is being used by another process.'
after clicking the save button, i get the error.
下面是我代码:'公共子SaveButton_Click(sender作为对象,e作为EventArgs)处理SaveButton。单击
Dim myStream As Stream
Dim FileSaveLocation As String
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "txt files (*.txt)|*.txt"
openFileDialog1.FilterIndex = 2
If openFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = openFileDialog1.OpenFile()
FileSaveLocation = openFileDialog1.FileName
MessageBox.Show(FileSaveLocation)
If (myStream IsNot Nothing) Then
Dim file As System.IO.StreamWriter
IO.File.WriteAllText(FileSaveLocation, "SETTINGS.txt")
file.WriteLine("list of variables and text go here, hidden for privacy" ,True)
File.Close()
End If
End If
End Sub`
我一直在切换代码,慢慢地使我的方式通过不同的问题和错误,我想也许它有一个奇怪的错误与messagebox,但删除没有区别,但我真的难倒了这一点,有人能帮助吗?谢谢吨提前,它伤害了我的大脑xd
1条答案
按热度按时间ubby3x7f1#
你要打开文件...
...然后调用WriteAllText,后者也尝试打开该文件...
如果您确实需要在写入之前打开文件以评估某些条件,那么您需要确保在调用WriteAllText之前关闭myStream