系统方法,判断文件是否打开并可选择关闭
This commit is contained in:
parent
bf861d5c75
commit
a75e7c15ca
36
Falcon.SugarApi.Windows/FileExtend.cs
Normal file
36
Falcon.SugarApi.Windows/FileExtend.cs
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
namespace Falcon.SugarApi.Windows
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 文件操作扩展
|
||||||
|
/// </summary>
|
||||||
|
public static class FileExtend
|
||||||
|
{
|
||||||
|
[DllImport("kernel32.dll")]
|
||||||
|
private static extern IntPtr _lopen(string lpPathName,int iReadWrite);
|
||||||
|
|
||||||
|
[DllImport("kernel32.dll")]
|
||||||
|
private static extern bool CloseHandle(IntPtr hObject);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 判断文件是否打开,并可选是否关闭
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">文件路径</param>
|
||||||
|
/// <param name="close">True关闭文件,不关闭。默认不关闭</param>
|
||||||
|
/// <returns>True文件打开,False文件没有打开</returns>
|
||||||
|
public static bool IsOpen(this string path,bool close = false) {
|
||||||
|
if(!File.Exists(path)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
var handle = _lopen(path,2|0x40);
|
||||||
|
if(handle==new IntPtr(-1)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(close) {
|
||||||
|
CloseHandle(handle);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user