I have placed a text file in c:\my_files\test1.txt
. Using C# I need to check whether the file is in an open state. If it is, I need to close the editor.
In the below code it doesn't go to the catch block if the file is in open state.
string path = @"c:\my_files\test1.txt";
FileStream stream = null;
try
{
stream = File.Open( path, FileMode.Open, FileAccess.Read, FileShare.None);
}
catch (IOException)
{
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
//return true;
Console.WriteLine("true");
}
finally
{
if (stream != null)
stream.Close();
}