Before you manipulate a file, you want to get the status of the current file, avoid opening the file again, the Code reference network has been written, shared with yourself as a record, but also as a reference for others.
#regionGet file Status[DllImport ("Kernel32.dll")] Private Static externIntPtr _lopen (stringLppathname,intireadwrite); [DllImport ("Kernel32.dll")] Private Static extern BOOLCloseHandle (IntPtr hobject); Private Const intOf_readwrite=2; Private Const intOf_share_deny_none=0x40; Private Static ReadOnlyIntPtr hfile_error=NewINTPTR (-1); Private Static intGetfilestatus (stringfilefullname) { if(!file.exists (Filefullname)) { return-1;//File NOT exists} INTPTR handle=_lopen (filefullname,of_readwrite|Of_share_deny_none); if(handle==hfile_error) { return 1;//Aready Open} closehandle (handle); return 0;//Not Open } #endregion
You need to reference a namespace:
using System.Runtime.InteropServices;
Windows Get File status