checking if file is used by other application
I am trying to open and read a file that has been written by other application. I am trying to check if the file is stile open by other application or not. If it is open I will check back periodically if it is close I will go a head and open the file and use.
I am writing a C# application. I am new to it.
Can anyone help me please?
Thanks,
Abiy
Abiy DemilewPosted Aug 5, 2007, 2:37 AM
Jan MontanoPosted Aug 4, 2007, 2:46 AM
I'm not sure if the code will result in an exception if the file was opened for read purposes only, but if it does, you can have a while loop like this:
bool isSuccessful = false;
while(!isSuccessful)
{
}
Abiy DemilewPosted Aug 3, 2007, 6:06 PM
In fact, if the file is opened, I want to try again. I don't want the applicaton to crash. So is there any way that will check if the file is opened or not without crashing my application, something like using loop.
Thanks,
Munir ShaikhPosted Aug 3, 2007, 2:05 AM
This should work with you,
bool opened = false;
string file = "h:\\Doc1.docx";
try
{
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
fs.Close();
}
catch (Exception)
{
opened = true;
}
finally
{
if (opened)
Console.WriteLine("File is opened!");
}
Enjoy!!
>>Munnamax