Reading a File Used by Another Process

Reading a file used by another process

If notepad can read the file then so can you, clearly the program didn't put a read lock on the file. The problem you're running into is that StreamReader will open the file with FileShare.Read. Which denies write access. That can't work, the other program already gained write access.

You'll need to create the StreamReader like this:

using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var sr = new StreamReader(fs, Encoding.Default)) {
// read the stream
//...
}

Guessing at the Encoding here. You have to be careful with this kind of code, the other program is actively writing to the file. You won't get a very reliable end-of-file indication, getting a partial last line is quite possible. In particular troublesome when you keep reading the file to try to get whatever the program appended.

C#: How to read a file that's in use by another process

Access the file in read mode so that you will not get this error.

Try this solution

How do I open an already opened file with a .net StreamReader?

Read log file being used by another process

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

How can I read a file even when getting an in use by another process exception?

FileStream logFileStream = new FileStream("c:\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader logFileReader = new StreamReader(logFileStream);

while (!logFileReader.EndOfStream)
{
string line = logFileReader.ReadLine();
// Your code here
}

// Clean up
logFileReader.Close();
logFileStream.Close();

Original source for code

How to read a file that another process is writing to

See the link here:

Reading a file which is locked by another process

Here is the code from that link:

using (FileStream logFileStream = new FileStream("c:\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (StreamReader logFileReader = new StreamReader(logFileStream))
{

while (!logFileReader.EndOfStream)
{
string line = logFileReader.ReadLine();
// Your code here
}
}

Read file that is used by another process

If the process holds an exclusive lock on the file then no. If the process holds a shared lock, you can read it.

How to access a text file in c# that is being used by another process

You can use a FileStream to open a file that is already open in another application. Then you'll need a StreamReader if you want to read it line by line. This works, assuming a file encoding of UTF8:

using (var stream = new FileStream(@"c:\tmp\locked.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
string line;

while ((line = reader.ReadLine()) != null)
{
// Do something with line, e.g. add to a list or whatever.
Console.WriteLine(line);
}
}
}

Alternative in case you really need a string[]:

var lines = new List<string>();

using (var stream = new FileStream(@"c:\tmp\locked.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
string line;
while ((line = reader.ReadLine()) != null)
{
lines.Add(line);
}
}
}

// Now you have a List<string>, which can be converted to a string[] if you really need one.
var stringArray = lines.ToArray();


Related Topics



Leave a reply



Submit