FileInfo로 확인할수 없는 해당 파일을 이미 오픈되었는지 확인하는 소스입니다.

구글링 끝에 찾아 내었습니다.

참고하시기 바랍니다.

출처 : http://stackoverflow.com/questions/876473/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;
}

 

   1:  protected virtual bool IsFileLocked(FileInfo file)
   2:  {
   3:      FileStream stream = null;
   4:      try
   5:      {
   6:          stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
   7:      }
   8:      catch (IOException)
   9:      {
  10:          //the file is unavailable because it is:
  11:          //still being written to
  12:          //or being processed by another thread
  13:          //or does not exist (has already been processed)
  14:          return true;
  15:      }
  16:      finally
  17:      {
  18:      if (stream != null)
  19:          stream.Close();
  20:      }
  21:      //file is not locked
  22:      return false;
  23:  }
 

 

 

+ Recent posts