1

尝试删除/创建/写入此文件时遇到问题。

错误:

The process cannot access the file 'C:\Users\Administrador\AppData\Local\Temp\PlayList_temp.txt' because it is being used by another process.

调试器突出显示的行:

If System.IO.File.Exists(Temp_file) = True Then System.IO.File.Delete(Temp_file)
System.IO.File.Create(Temp_file)

(两者中的任何一个,如果我删除一行,另一行采用相同的错误 id)

子:

Public Sub C1Button2_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If Not playerargs = Nothing Then

        Dim Str As String
        Dim Pattern As String = ControlChars.Quote
        Dim ArgsArray() As String
        Dim Temp_file As String = System.IO.Path.GetTempPath & "\PlayList_temp.txt"
        Dim objWriter As New System.IO.StreamWriter(Temp_file)

        Str = Replace(playerargs, " " & ControlChars.Quote, "")
        ArgsArray = Split(Str, Pattern)

        If System.IO.File.Exists(Temp_file) = True Then System.IO.File.Delete(Temp_file)
        System.IO.File.Create(Temp_file)

    For Each folder In ArgsArray
        If Not folder = Nothing Then
            Dim di As New IO.DirectoryInfo(folder)
            Dim files As IO.FileInfo() = di.GetFiles("*")
                Dim file As IO.FileInfo

            For Each file In files
                ' command to writleline
                'Console.WriteLine("File Name: {0}", file.Name)
                'Console.WriteLine("File Full Name: {0}", file.FullName)
                objWriter.Write(file.FullName)
                objWriter.Close()
            Next
        End If
        Next

    If randomize.Checked = True Then
            RandomiseFile(Temp_file)
    End If

    Process.Start(userSelectedPlayerFilePath, playerargs)
    If autoclose.Checked = True Then
        Me.Close()
    End If
    Else
    MessageBox.Show("You must select at least one folder...", My.Settings.APPName)
    End If
End Sub
4

3 回答 3

1

首先,File.Create创建或覆盖指定的文件,因此您之前不需要删除它。
其次,像您一样初始化StreamWriter会阻塞文件,因此您无法在之后重新创建它。
因此,只需在 File.Create 之后移动 StreamWriter 初始化,或者更好的是,完全删除 File.Create 并使用覆盖文件的StreamWriter 重载

....
If Not playerargs = Nothing Then
    ....
    Dim Temp_file As String = System.IO.Path.GetTempPath & "\PlayList_temp.txt"

    Using objWriter As New System.IO.StreamWriter(Temp_file, false)
        For Each folder In ArgsArray
            If Not folder = Nothing Then
                Dim di As New IO.DirectoryInfo(folder)
                Dim files As IO.FileInfo() = di.GetFiles("*")
                Dim file As IO.FileInfo
                For Each file In files
                    ' command to writleline
                    'Console.WriteLine("File Name: {0}", file.Name)
                    'Console.WriteLine("File Full Name: {0}", file.FullName)
                    objWriter.Write(file.FullName)
                    ' objWriter.Close() 
                Next
            End If
        Next
    End Using ' Flush, close and dispose the objWriter
    ....

还注意到您在循环内关闭了编写器,在工作完成后使用 Using 语句正确关闭编写器。

于 2012-11-22T10:45:12.000 回答
1
The process cannot access the file 'C:\Users\Administrador\AppData\Local\Temp\PlayList_temp.txt' because it is being used by another process.

这意味着该文件已打开或被另一个进程使用。

打开您的任务管理器并检查文件是否存在,然后将其关闭或单工结束进程。

我有同样的问题,但我通过关闭文件解决了。

希望这可以帮助!

于 2012-11-22T10:46:42.277 回答
1

从您的详细信息来看,如果文件存在并创建一个新文件,您似乎愿意删除。

我建议删除这一行:

 System.IO.File.Create(Temp_file)

并将这条线移动到流写入器线上:

If System.IO.File.Exists(Temp_file) = True Then System.IO.File.Delete(Temp_file)

这样的事情会帮助你:

部分代码:

Dim Temp_file As String = System.IO.Path.GetTempPath & "\PlayList_temp.txt"

' Delete file if exists
If System.IO.File.Exists(Temp_file) = True Then System.IO.File.Delete(Temp_file)    

' Create file for write.
Dim objWriter As New System.IO.StreamWriter(Temp_file)

Str = Replace(playerargs, " " & ControlChars.Quote, "")
ArgsArray = Split(Str, Pattern)
...
...
...
于 2012-11-22T10:54:12.007 回答