3

Possible Duplicate:
Is there a way to check if a file is in use?
Check if a file is open

how can i know file is already open or in use.

public bool FileIsLocked(string strFullFileName)
        {
            bool blnReturn = false;
            System.IO.FileStream fs;
            try
            {
                fs = System.IO.File.Open(strFullFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
                fs.Close();
            }
            catch (System.IO.IOException ex)
            {
                blnReturn = true;
            }
            return blnReturn;
        }

I find above code it is not working properly

Community
  • 1
  • 1
bhaveshkac
  • 477
  • 2
  • 8
  • 15
  • http://stackoverflow.com/questions/1304/how-to-check-for-file-lock-in-c – vulkanino Feb 14 '12 at 13:14
  • 1
    Note that the information you get using a function like that, can be obsolete the moment you use it. The file can be locked, the instance you checked that it wasn't and vice versa. Usually it is best to try whatever you need to do with the file, and react to errors when they occur. – Christian.K Feb 14 '12 at 13:18
  • i get this code but it is not work proparly – bhaveshkac Feb 14 '12 at 13:21
  • dup of a dup? http://stackoverflow.com/questions/3162957/determine-if-a-file-is-open – kenny Feb 14 '12 at 13:26

3 Answers3

0

As taken from here: Is there a way to check if a file is in use?

protected virtual bool IsFileLocked(FileInfo file)
    {
        FileStream stream = null;

        try
        {
            stream = file.Open(FileMode.Open, FileAccess.ReadWrite, 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;
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }

        //file is not locked
        return false;
    }
Community
  • 1
  • 1
Seany84
  • 5,526
  • 5
  • 42
  • 67
0

I Already answers before over here : Check if a file is open

Edit

FileInfo file = new FileInfo(path);

Function

protected virtual bool IsFileinUse(FileInfo file)
{
     FileStream stream = null;

     try
     {
         stream = file.Open(FileMode.Open, FileAccess.ReadWrite, 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;
     }
     finally
     {
         if (stream != null)
         stream.Close();
     }
     return false; 
}
Community
  • 1
  • 1
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
0

Try catching UnauthorizedAccessException as per http://msdn.microsoft.com/en-us/library/b9skfh7s.aspx

UnauthorizedAccessException doesn't inherit from IOException http://msdn.microsoft.com/en-us/library/system.unauthorizedaccessexception.aspx

Oliver
  • 1,490
  • 18
  • 19