C # determine whether a file is opened or not

Source: Internet
Author: User
C # determine whether the file is opened and occupied by using system. io; using system. runtime. interopservices; [dllimport ("kernel32.dll")] public static extern intptr _ lopen (string lppathname, int ireadwrite); [dllimport ("kernel32.dll")] public static extern bool closehandle (intptr hobject); Public const int of_readwrite = 2; Public const int of_1__deny_none = 0x40; Public readonly intptr hfile_error = new intptr (-1 ); private void button#click (o Bject sender, eventargs e) {string vfilename = @ "C: \ temp \ temp.bmp"; if (! File. exists (vfilename) {MessageBox. show ("the file does not exist, so you should stop playing with it"); return;} intptr vhandle = _ lopen (vfilename, of_readwrite | of_share_deny_none); If (vhandle = hfile_error) {MessageBox. show ("file occupied! "); Return;} closehandle (vhandle); MessageBox. Show (" not occupied! ");}

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.