For applications, you may need to determine whether a file has been opened or not. This is especially important in programs that frequently read and write files, because a file can only have one stream connection at a time. The following code may be helpful. [Csharp] public class FileStatus {[DllImport ("kernel32.dll")] private static extern IntPtr _ lopen (string lpPathName, int iReadWrite); [DllImport ("kernel32.dll")] private static extern bool CloseHandle (IntPtr hObject); private const int OF_READWRITE = 2; private const int of_1__deny_none = 0x40; private static readonly IntPtr HFILE_ERROR = new IntPtr (-1 ); public static int FileIsOpen (string fil EFullName) {if (! File. exists (fileFullName) {return-1;} IntPtr handle = _ lopen (fileFullName, OF_READWRITE | of_assist_deny_none); if (handle = HFILE_ERROR) {return 1 ;} closeHandle (handle); return 0 ;}} public class FileStatus {[DllImport ("kernel32.dll")] private static extern IntPtr _ lopen (string lpPathName, int iReadWrite ); [DllImport ("kernel32.dll")] private static extern bool CloseHandle (IntPtr hObject ); Private const int OF_READWRITE = 2; private const int OF_SHARE_DENY_NONE = 0x40; private static readonly IntPtr HFILE_ERROR = new IntPtr (-1); public static int FileIsOpen (string fileFullName) {if (! File. exists (fileFullName) {return-1;} IntPtr handle = _ lopen (fileFullName, OF_READWRITE | of_assist_deny_none); if (handle = HFILE_ERROR) {return 1 ;} closeHandle (handle); return 0 ;}} test: [csharp] class Program {static void Main (string [] args) {string testFilePath = AppDomain. currentDomain. setupInformation. applicationBase + @ "testOpen.txt"; FileStream fs = new FileStream (testFilePath, FileMode. openOrCreate, FileAccess. read); BinaryReader br = new BinaryReader (fs); br. read (); Console. writeLine ("file opened"); int result = FileStatus. fileIsOpen (testFilePath); Console. writeLine (result); br. close (); Console. writeLine ("file closed"); result = FileStatus. fileIsOpen (testFilePath); Console. writeLine (result); Console. readLine () ;}} class Program {static void Main (string [] args) {string testFilePath = AppDomain. currentDomain. setupInformation. applicationBase + @ "testOpen.txt"; FileStream fs = new FileStream (testFilePath, FileMode. openOrCreate, FileAccess. read); BinaryReader br = new BinaryReader (fs); br. read (); Console. writeLine ("file opened"); int result = FileStatus. fileIsOpen (testFilePath); Console. writeLine (result); br. close (); Console. writeLine ("file closed"); result = FileStatus. fileIsOpen (testFilePath); Console. writeLine (result); Console. readLine () ;}} result: