Tuesday, June 10, 2014

C#.NET - Check whether file closed

if (!IsFileClosed(sLocalCurrentDocumentPath))
{
MessageBox.Show("Document opened already..!", clsCommon.sProjectName, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}

public static bool IsFileClosed(string filename)
{
try
{
using (var inputStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.None))
{
                    return true;
            }
}
catch (IOException)
{
return false;
      }
}

No comments:

Post a Comment