错误:另一个进程正在使用该文件

时间:2014-10-07 20:31:28

标签: vb.net

我有一个问题,即文件已被其他进程使用

但我很确定它没有被其他地方使用..

Imports System.IO
Public Class Browse
    Private Sub add_Click(sender As Object, e As EventArgs) Handles add.Click
        Dim Files As New OpenFileDialog
        Dim Folder As New FolderBrowserDialog
        Dim Filelist As String = "C:\Program Files\FTP-Sync\Files.txt"
        Dim FileP As String = ""
        Dim list() As String = IO.File.ReadAllLines(Filelist)

        If fileb.Checked Then
            Try
                If System.IO.File.Exists(Filelist) = True And Files.ShowDialog = Windows.Forms.DialogResult.OK Then
                    FileP = Files.FileName
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If

        If folderb.Checked Then
            Try
                If System.IO.File.Exists(Filelist) = True And Folder.ShowDialog = Windows.Forms.DialogResult.OK Then
                    FileP = Folder.SelectedPath
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
        pathtxt.Text = FileP
    End Sub

    Private Sub ok_Click(sender As Object, e As EventArgs) Handles ok.Click
        Dim Filelist As String = "C:\Program Files\FTP-Sync\Files.txt"
        Dim writer As StreamWriter = New StreamWriter(Filelist)
        Dim FileP As String = ""
        Try
            If pathtxt.Text = "" Then
                MsgBox("No folder or file has been choosen")
            Else
                writer = File.AppendText(Filelist)
                writer.WriteLine(pathtxt.Text)
                pathtxt.Text = FileP
                writer.Close()
                Me.Hide()
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


    End Sub

    Private Sub cancel_Click(sender As Object, e As EventArgs) Handles cancel.Click
        Me.Hide()
    End Sub

    Private Sub Browse_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class

提前致谢!

1 个答案:

答案 0 :(得分:0)

正因为如此:

Dim writer As StreamWriter = New StreamWriter(Filelist)

我把它改成了

Dim writer As StreamWriter

它的工作就像一个魅力..不知道它是否是正确的方式......但它确实有用。