Read log file being used by another process

前端 未结 2 1751
南旧
南旧 2020-12-31 02:37

Goal

I want to press a button on my GUI and read in the seclog.log file (symantec AV log) from a remote machine and display the contents of the log

相关标签:
2条回答
  • 2020-12-31 03:15
    //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();
    
    
    }
    

    what i had to change

    i had to create a readwrite filestream

    original code

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

    new code

    Stream stream = File.Open(seclogPath1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    //File.OpenRead(seclogPath1);
    StreamReader streamReader = new StreamReader(stream);
    
    0 讨论(0)
  • 2020-12-31 03:20
    using (StreamReader sr = new StreamReader(filePath, true))
    {
       sr.Close(); //This is mandatory
       //Do your file operation
    }
    
    0 讨论(0)
提交回复
热议问题