using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace AdminTools
{
public static class FileTools
{
[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);
/// <summary>
/// 检查文件是否已经打开
/// </summary>
/// <param name="strfilepath">要检查的文件路径</param>
/// <returns>true文件已经打开,false文件可用未打开</returns>
public static bool FileUsing(string strfilepath)
{
string vFileName = strfilepath;
if (!System.IO.File.Exists(vFileName))
{
return true;
}
IntPtr vHandle = _lopen(vFileName, OF_READWRITE | OF_SHARE_DENY_NONE);
if (vHandle == HFILE_ERROR)
{
return true;
}
CloseHandle(vHandle);
return false;
}
}
}