2012-10-17 14 views
16

bramkiPrzeczytaj plik dziennika jest używany przez inny proces

chcę nacisnąć przycisk na moim GUI i czytać w pliku dziennika seclog.log (Symantec AV) z komputera zdalnego i wyświetlania zawartości katalogu zaloguj się do pola tekstowego w mojej aplikacji.

rzeczy, które działają

wszystko ale czyta plik dziennika

Komunikat o błędzie

System.IO.IOException was unhandled 
Message=The process cannot access the file '\\HOSTNAME\C$\Program Files (x86)\Symantec\Symantec Endpoint Protection\seclog.log' because it is being used by another process. 
Source=mscorlib 

kod

//possible seclog paths 
     String seclogPath1 = @"\\\\" + target + "\\C$\\Program Files (x86)\\Symantec\\Symantec Endpoint Protection\\seclog.log"; 
     String seclogPath2 = @"\\\\" + target + "\\C$\\Program Files\\Symantec\\Symantec Endpoint Protection\\seclog.log"; 

     //if seclog exists 
     if (File.Exists(seclogPath1)) 
     { 
      //output.AppendText("file exists at " + seclogPath1); 
      //var seclogContent = File.Open(seclogPath1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 

      Stream stream = File.OpenRead(seclogPath1); 
      StreamReader streamReader = new StreamReader(stream); 
      string str = streamReader.ReadToEnd(); 
      output.AppendText(str); 
      streamReader.Close(); 
      stream.Close(); 


     } 

miejsca Próbowałem

File is being used by another process

C# The process cannot access the file ''' because it is being used by another process

Googling problem

korzystając filestreams na wiele sposobów

Odpowiedz

31
//possible seclog paths 
     String seclogPath1 = @"\\\\" + target + "\\C$\\Program Files (x86)\\Symantec\\Symantec Endpoint Protection\\seclog.log"; 
     String seclogPath2 = @"\\\\" + target + "\\C$\\Program Files\\Symantec\\Symantec Endpoint Protection\\seclog.log"; 

     //if seclog exists 
     if (File.Exists(seclogPath1)) 
     { 
      //output.AppendText("file exists at " + seclogPath1); 
      //var seclogContent = File.Open(seclogPath1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 

      Stream stream = File.Open(seclogPath1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
       //File.OpenRead(seclogPath1); 
      StreamReader streamReader = new StreamReader(stream); 
      string str = streamReader.ReadToEnd(); 
      output.AppendText(str); 
      streamReader.Close(); 
      stream.Close(); 


     } 

co miałem do Chang e

miałem aby utworzyć ReadWrite FILESTREAM

oryginalny kod

Stream stream = File.OpenRead(seclogPath1); 
     StreamReader streamReader = new StreamReader(stream); 

nowy kod

Stream stream = File.Open(seclogPath1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
       //File.OpenRead(seclogPath1); 
      StreamReader streamReader = new StreamReader(stream); 
+0

Nicea, że ​​znalazł rozwiązanie. Chciałam podać link do posta z tym samym numerem: http: //stackoverflow.com/questions/3560651/whats-the-least-invasive-way-to-read-a-locked-file-in- c-sharp-maybe-in-unsaf – joshgo

+0

@joshgo thanks! Wpatrywałem się w kod i na godzinę, zanim to rozgryzłem. lol – toosweetnitemare

+0

To rozwiązanie sprawdziło się przy implementacji Serilog. W podobnej sytuacji musieliśmy przeczytać część pliku dziennika, aby przedstawić ją w interfejsie internetowym, ale odczyt nie powiódł się, to rozwiązało. –

0
using (StreamReader sr = new StreamReader(filePath, true)) 
{ 
    sr.Close(); //This is mandatory 
    //Do your file operation 
} 
+0

Użycie powinno zamknąć to za Ciebie? – Danh

+0

Próbowałem, ale to nie jest zamykanie. Muszę więc jawnie wywołać .close. – Kurkula

+0

Problem powinien być gdzieś indziej https://msdn.microsoft.com/en-us/library/yh598w02.aspx – Danh

Powiązane problemy