Read log file being used by another process
Asked Answered
C

2

27

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 to a rich text box in my application.

Things That Work

everything but reading the log file

Error Message

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

code

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


        }

Things I've Tried

File is being used by another process

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

Googling the issue

using filestreams in multiple ways

Comradery answered 17/10, 2012 at 20:20 Comment(0)
C
46
//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);
Comradery answered 17/10, 2012 at 20:24 Comment(4)
Nice, you found the solution. I was going to give you a link to a post with the same issue:#3561151Tingey
@Tingey thanks! i was staring at the code for like and hour before i figured that out. lolComradery
This solution worked for me with an implementation of Serilog. Similar situation, we needed to read a section of the log file to present in a web front end, but the read failed, this resolved it.Thoraco
if doesn't exists the file?Bondswoman
K
1
using (StreamReader sr = new StreamReader(filePath, true))
{
   sr.Close(); //This is mandatory
   //Do your file operation
}
Kahaleel answered 17/11, 2016 at 0:29 Comment(3)
The using should close it for you?Irrecoverable
I tried but it's not closing. So I have to explicitly call .close.Kahaleel
Problem should be somewhere else msdn.microsoft.com/en-us/library/yh598w02.aspxIrrecoverable

© 2022 - 2024 — McMap. All rights reserved.