StreamWriter - Can not access the file because it is being used by another process

1

I have the following code below that when run it always returns me the error that is being used by another process. I guarantee there is no other process open with it!

#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 in StreamWriter . Can anyone help me?

    
asked by anonymous 04.06.2015 / 00:46

1 answer

1

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

The problem occurs in the EscreverProcesso method:

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 solve, in the above code replace the line:

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

By these lines:

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

Always when you are opening a file, either for reading or writing , you must close the identifier object with method StreamWriter.Close .

    
04.06.2015 / 01:36