using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.IO; namespace demo.ClassHelper.FileOperate { class FileOpen { [DllImport("kernel32.dll")] private static extern IntPtr _lopen(string lpPathName, int iReadWrite); [DllImport("kernel32.dll")] private static extern bool CloseHandle(IntPtr hObject); public static int OF_READWRITE = 2; public static int OF_SHARE_DENY_NONE = 0x40; public static IntPtr HFILE_ERROR = new IntPtr(-1); //txt文件没有handle; public static int VerifyFileIsOpen(string strFileName) { if (!File.Exists(strFileName)) { return -1; } IntPtr vHandle = _lopen(strFileName, OF_READWRITE | OF_SHARE_DENY_NONE); if (vHandle == HFILE_ERROR) {//文件已经打开 return 1; } CloseHandle(vHandle); //文件未打开,并且可用 return 0; } } }