Streamwriter - Cannot access the file because it is being used by another process

Asked

Viewed 1,405 times

1

Guys I have the following code below that when executed it always returns me the error that is being used by another process. I guarantee there’s no other lawsuit opened with him!

#Region "Declaração DLL"
    Dim ServerMaster As ServiceNetwork_ServerMasterSecurity
#End Region

#Region "Escritor"

#End Region

#Region "Declaração De String's"
    Dim LocalAPPProc As String = Application.StartupPath & "\Server Master\AntiDebugger\Cache Process Kill"
#End Region

#Region "Date"
    Dim Data As DateTime = Date.Now
#End Region

    Public Sub IniciarProcessoFolder()
        If Directory.Exists(LocalAPPProc & "\" & Data.Year) Then
            VerificarSubPastas()
        Else
            Directory.CreateDirectory(LocalAPPProc & "\" & Data.Year)
            IniciarProcessoFolder()
        End If
    End Sub

    Private Sub VerificarSubPastas()
        Dim Local As String = LocalAPPProc & "\" & Data.Year.ToString()
        Dim MesFolder As Integer = 12

        For i As Integer = 1 To MesFolder

            If Directory.Exists(Local & "\" & i) Then
                i += 1
            Else
                Directory.CreateDirectory(Local & "\" & i)
                Thread.Sleep(20)
            End If

            If i = 12 Then
                VerificarStringProcess()

            End If
        Next
    End Sub

    Private Sub VerificarStringProcess()
        Dim LerProcessKill As String() = File.ReadAllLines(LocalAPPProc & "\ProcKill.lg")

        ''Primeiro Verificar Se Tem Alguma String
        If LerProcessKill(0) <> "" Then

            EscreverProcesso()

        Else

            MsgBox("O Processo não pode ser gravado no cache por não conter string valida.", MsgBoxStyle.Exclamation, "AntiDebuggers")

        End If
    End Sub

    Private Sub EscreverProcesso()
        Dim Dia As String = Data.DayOfWeek
        Dim Mes As String = Data.Month.ToString()
        Dim Ano As String = Data.Year.ToString()
        Dim Hora As String = Data.Hour & "." & Data.Minute.ToString()
        Dim Seconds As String = Data.Second.ToString()

        Dim DateComplete As String = Dia & "." & Mes & "." & Ano & "." & Hora & "." & Seconds & ".log"

        Dim LC As String = LocalAPPProc & "\" & Ano & "\"

        If Directory.Exists(LocalAPPProc & "\" & Data.Year) Then

            ''Cria o arquivo de log 
            File.CreateText(LC & Mes & "\" & DateComplete)

            ''Chama o escritor
            Escreve()
        Else
            MsgBox("A Pasta de log's não existe.", MsgBoxStyle.Exclamation, "AntiDebuggers:0x003")
        End If
    End Sub

    Private Sub Escreve()
        Dim Dia As String = Data.DayOfWeek
        Dim Mes As String = Data.Month.ToString()
        Dim Ano As String = Data.Year.ToString()
        Dim Hora As String = Data.Hour & "." & Data.Minute.ToString()
        Dim Seconds As String = Data.Second.ToString(0)


        Dim LC As String = LocalAPPProc & "\" & Ano & "\"
        Dim DateComplete As String = Dia & "." & Mes & "." & Ano & "." & Hora & "." & Seconds & ".log"
        Dim Escritor As StreamWriter
        Escritor = New StreamWriter(LC & Mes & "\" & DateComplete) ''<< Aqui é o problema
        Dim ProcessKill As String() = System.IO.File.ReadAllLines(Application.StartupPath & "\Server Master\AntiDebugger\Cache Process Kill\ProcKill.lg")


        Escritor.WriteLine("Process Kill:   " & ProcessKill(0))
        Escritor.WriteLine("Date Kill: " & Date.Now)
        Escritor.Close()

        ''Reinicia o timer do server master
        '   Dim Serv As New ServiceNetwork_ServerMasterSecurity

        ' Serv.TAntiDebugger.Start()
    End Sub

The problem occurs when I declare a new Instance at StreamWriter. Can someone help me?

1 answer

1


In fact there is no other process using the file, it is your own program.

The problem occurs in the method EscreverProcesso:

Private Sub EscreverProcesso()
   ' ....
   If Directory.Exists(LocalAPPProc & "\" & Data.Year) Then
        File.CreateText(LC & Mes & "\" & DateComplete)
        ' Aqui é necessário você fechar o objeto do StreamWriter!
        ' ...
        Escreve() ' Quando você chama este método, ocasiona o erro!
        ' ....
   End If
End Sub

To resolve, in the above code replace the line:

File.CreateText(LC & Mes & "\" & DateComplete)

Along these lines:

Dim sw As StreamWriter = File.CreateText(LC & Mes & "\" & DateComplete)
sw.Close()

Whenever you are going to open a file, either to reading or writing, it is necessary to close the identifier object with the method StreamWriter.Close.

  • 1

    Thank you very much!! Really I never thought it would be for that.. I thought that method did not occupy the file so..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.