C # how to determine whether a file is open

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.