繁体   English   中英

我试图覆盖的文本文件正在被另一个进程使用,但它没有被使用?

[英]text file im trying to overwrite is being used by another process, but it is not in use?

我正在尝试使用 vb.net winforms 中的打开文件对话框覆盖保存在外部驱动器上的文本文件,但我不断收到错误消息: System.IO.IOException: 'The process cannot access the file 'F:\SETTINGS.TXT' because it is being used by another process.' 单击保存按钮后,出现错误。

这是我的代码:` Public Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click

    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`

我一直在切换代码并慢慢解决不同的问题和错误,我认为消息框可能有一个奇怪的错误但删除它没有任何区别,但我真的很难过这个,有人可以帮忙吗? 提前致谢,它伤害了我的大脑 XD

你打开文件...

myStream = openFileDialog1.OpenFile()

...然后调用 WriteAllText 来尝试打开文件...

IO.File.WriteAllText(FileSaveLocation, "SETTINGS.txt")

如果您确实需要在写入之前打开文件以评估某些条件,那么您需要确保在调用 WriteAllText 之前关闭 myStream

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM