File state check - different program behavior depending on server

  • Thread starter Thread starter ms_bi_pete
  • Start date Start date
M

ms_bi_pete

Guest
Hi everybody,

I have written a program to check if it is possible to open a file in C#.
If my file is opened in another program (a text editor for instance), it should not be possible and return false, it returns true otherwise.

It works on my computer but not on my client server.

protected virtual bool IsFileLocked(FileInfo file)
{
try
{
using(FileStream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None))
{
stream.Close();
}
}
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;
}

//file is not locked
return false;
}

Thanks for your help

Pete

Continue reading...
 
Back
Top