I am trying to read a text file from a folder, but currently that file is being accessed by another process. Therefore I am getting an exception that the "...File is being accessed by another process".
I am using the following code
class program
{
static void Main(string[] args)
{
string lFileData = string.Empty;
try
{
if (args.Count() > 0)
lFileData = ReadLogFile(args[0]);
Console.WriteLine("{0}", lFileData);
}
catch(Exception ex)
{
Console.WriteLine("{0}", ex.Message.ToString());
}
Console.ReadLine();
}
public static string ReadLogFile(string sFileLocation)
{
string lResult = string.Empty;
var inStream = new FileStream(sFileLocation, FileMode.Open, FileAccess.Read, FileShare.Read);
using (var reader = new StreamReader(inStream))
{
lResult= reader.ReadToEnd();
}
return lResult;
}
}
But when we open the same file through notepad is display the contents of the text file even though file is being accessed by another process.
We want to do the same - read the content of the file without any modification of it. Therefore I tried this:
var inStream = new FileStream(sFileLocation, FileMode.Open, FileAccess.Read, FileShare.Read);
However, even then we are getting same exception.
Can anybody help in resolving this issue?