From a75e7c15ca217e88532bdb87a21afd058d2f6f4f Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Thu, 2 Mar 2023 10:40:29 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E6=96=87=E4=BB=B6=E6=98=AF=E5=90=A6=E6=89=93?= =?UTF-8?q?=E5=BC=80=E5=B9=B6=E5=8F=AF=E9=80=89=E6=8B=A9=E5=85=B3=E9=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.SugarApi.Windows/FileExtend.cs | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Falcon.SugarApi.Windows/FileExtend.cs diff --git a/Falcon.SugarApi.Windows/FileExtend.cs b/Falcon.SugarApi.Windows/FileExtend.cs new file mode 100644 index 0000000..b67047c --- /dev/null +++ b/Falcon.SugarApi.Windows/FileExtend.cs @@ -0,0 +1,36 @@ +using System.Runtime.InteropServices; + +namespace Falcon.SugarApi.Windows +{ + /// + /// 文件操作扩展 + /// + 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); + + /// + /// 判断文件是否打开,并可选是否关闭 + /// + /// 文件路径 + /// True关闭文件,不关闭。默认不关闭 + /// True文件打开,False文件没有打开 + 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; + } + } +}