2012-10-17 7 views
16

GoalLeggi file di log utilizzato da un altro processo

voglio premere un pulsante sul mio GUI e leggere nel file seclog.log (symantec log AV) da un computer remoto e visualizzare il contenuto del accedere a una casella di testo RTF nella mia applicazione.

cose che funzionano

tutto, ma la lettura del file di log

messaggio di errore

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 

codice

//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(); 


     } 

Le cose che ho provato

File is being used by another process

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

Googling il problema

utilizzando filestreams in molteplici modi

risposta

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(); 


     } 

quello che avevo da chang e

ho dovuto creare un filestream readwrite

codice originale

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

nuovo codice

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

Nizza, avete trovato la soluzione. Stavo per darti un link a un post con lo stesso problema: http: //stackoverflow.com/questions/3560651/whats-the-least-invasive-way-to-read-a-locked-file-in- c-sharp-maybe-in-unsaf – joshgo

+0

@joshgo grazie! stavo fissando il codice per come e un'ora prima ho capito. lol – toosweetnitemare

+0

Questa soluzione ha funzionato per me con un'implementazione di Serilog. Situazione simile, avevamo bisogno di leggere una sezione del file di log da presentare in un front-end Web, ma la lettura non è riuscita, questo l'ha risolto. –

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

L'utilizzo dovrebbe chiuderlo per te? – Danh

+0

Ho provato ma non si sta chiudendo. Quindi devo chiamare esplicitamente .close. – Kurkula

+0

Il problema dovrebbe essere da qualche altra parte https://msdn.microsoft.com/en-us/library/yh598w02.aspx – Danh