Visual Basic - 无法访问文件,因为即使在关闭文件后,另一个进程也会使用该文件

时间:2015-04-09 11:21:28

标签: vb.net file process streamwriter

我的脚本在通过程序创建和编辑后自动上传文本文件。创建和编辑(追加)工作正常,但当exe到达文件上传的行时,我收到错误:

  

由于其他进程使用

而无法访问文件

在上传之前文件正在关闭和处理,但这无关紧要。即使经过一些谷歌搜索,我也找不到问题和解决方案。

我使用以下代码创建并附加文本。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
    Try
        Using file As New System.IO.StreamWriter(currentdir + "\logs\" + FormClient.gametitle + "." + FormLogin.username + "_" + thisDate + "_ [" + fileNumber + "]" + ".txt", True)
            file.WriteLine(TxtIssue.Text + "      " + TxtWhen.Text + "      " + TxtWhere.Text + "      " + TxtInfo.Text)
            file.WriteLine("")
        End Using

    Catch ex As Exception
        MessageBox.Show(("Error while loading: " + ex.Message))
    End Try
End Sub

Private Sub FormFeedback_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles MyBase.FormClosing
    thisDate = Today
    Dim filenameFormat = currentdir + "\logs\" + FormClient.gametitle + "." + FormLogin.username + "_" + thisDate + "_ [" + fileNumber + "]" + ".txt"
    Dim uploadFormat = Path.Combine("-removed-", filenameFormat)
    My.Computer.Network.UploadFile(filenameFormat, uploadFormat, "mennovv", "mennomail98", True, 500)

End Sub

以下代码检查文件是否存在,但我不认为这是问题。

While My.Computer.FileSystem.FileExists(currentdir + "\logs\" + FormClient.gametitle + "." + FormLogin.username + "_" + thisDate + "_ [" + fileNumber + "]" + ".txt") = True
        fileNumber += 1
    End While

0 个答案:

没有答案