C# .NET 判断文件是否被占用

public class FileUtil
{
	[DllImport("kernel32.dll")]
	private static extern IntPtr _lopen(string lpPathName, int iReadWrite);

	[DllImport("kernel32.dll")]
	public static extern bool CloseHandle(IntPtr hHandle);


	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="filePath">文件路径</param>
	/// <returns></returns>
	public static bool IsOccupied(string filePath)
	{
		IntPtr vHandle = _lopen(filePath, OF_READWRITE | OF_SHARE_DENY_NONE);
		if(vHandle == HFILE_ERROR)
		{
			return true;
		}
		CloseHandle(vHandle);
		return false;
	}
}
—— 完 ——
相关推荐
评论

立 为 非 似

中 谁 昨 此

宵 风 夜 星

。 露 , 辰

文章点击榜

细 无 轻 自

如 边 似 在

愁 丝 梦 飞

。 雨 , 花