2012-11-22 4 views
1

이 파일을 삭제/작성/쓰려고 할 때 문제가 있습니다.VB.NET - 텍스트 파일을 쓰려고 할 때의 문제

는 오류 : 나는 한 줄을 삭제하면

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

(둘 중 하나가, 다른 라인은 동일한 오류 ID를 취

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

은 디버거에 의해 라인을 강조)

서브 :

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 

답변

1

첫째, File.Create 만들거나 당신이 전에 그것을 삭제 할 필요가 없습니다, 지정된 파일을 덮어 씁니다.
두 번째로 StreamWriter을 초기화하면 파일이 차단되므로 나중에 다시 만들 수 없습니다.
그래서, 단순히 File.Create 후 StreamWriter를의 초기화 데이터를 이동, 또는 더 나은, 전부 File.Create를 제거하고

당신이 내부에있는 동안 여전히 작가를 닫는 것이 아니라 드러내 파일

.... 
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 
    .... 
을 덮어 씁니다 StreamWriter overload를 사용 루프를 사용하면 작업이 완료된 후 Using 문을 사용하여 작성기를 올바르게 닫을 수 있습니다.

+0

정말 고맙습니다!! 덧글에 대한 – ElektroStudios

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

이는 파일이 열려 있거나 다른 프로세스에서 사용되었음을 의미합니다.

작업 관리자를 열고 파일이 있는지 확인한 다음 닫거나 프로세스를 종료하십시오.

나는 동일한 문제가 있었지만 파일을 닫음으로써 해결할 수있었습니다.

희망이 도움이됩니다.

+0

고마워요하지만 그게 문제가 아니에요 – ElektroStudios

1

자세한 내용은 파일이 있는지 확인하고 새 파일을 만드는 것 같습니다.

내가이 줄을 제거하기 위해 제안 :

System.IO.File.Create(Temp_file) 

을 그냥에서는 StreamWriter 회선을 통해이 줄을 이동 :이 같은

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) 
... 
... 
... 
+0

그 예제 코드 고마워요 – ElektroStudios

관련 문제