VB.NET - 试图编写文本文件的问题

时间:2012-11-22 10:38:39

标签: vb.net visual-studio text text-files streamwriter

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

错误:

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

3 个答案:

答案 0 :(得分:1)

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

....
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语句正确关闭编写器。

答案 1 :(得分:1)

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

这意味着该文件已被打开或被其他进程使用。

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

我遇到了同样的问题,但我通过关闭文件解决了这个问题。

希望这有帮助!

答案 2 :(得分: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)
...
...
...