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! ");}