简体   繁体   English

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

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

im trying to overwrite a text file saved on an external drive using an openfile dialog in 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.'我正在尝试使用 vb.net winforms 中的打开文件对话框覆盖保存在外部驱动器上的文本文件,但我不断收到错误消息: 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.单击保存按钮后,出现错误。

here is my code: ` Public Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click这是我的代码:` 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`

ive been switching around the code and slowly making my way through different issues and errors, i thought maybe it has a strange error with the messagebox but removing that makes no difference, but im really stumped on this one, can anyone help?我一直在切换代码并慢慢解决不同的问题和错误,我认为消息框可能有一个奇怪的错误但删除它没有任何区别,但我真的很难过这个,有人可以帮忙吗? thanks a tonne in advance, its hurting my brain XD提前致谢,它伤害了我的大脑 XD

You're opening the file...你打开文件...

myStream = openFileDialog1.OpenFile()

... and then calling WriteAllText which tries to open the file as well... ...然后调用 WriteAllText 来尝试打开文件...

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

If you truly do need to open the file to evaluate some condition before you write then you'll need to be sure to close myStream before the call to WriteAllText如果您确实需要在写入之前打开文件以评估某些条件,那么您需要确保在调用 WriteAllText 之前关闭 myStream

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

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