public class FileStatus
{
[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);
public static int FileIsOpen(string fileFullName)
{
if (!File.Exists(fileFullName))
{
return -1;
}
IntPtr handle = _lopen(fileFullName, OF_READWRITE | OF_SHARE_DENY_NONE);
if (handle == HFILE_ERROR)
{
return 1;
}
CloseHandle(handle);
return 0;
}
}
class Program
{
static void Main(string[] args)
{
string testFilePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"testOpen.txt";
FileStream fs = new FileStream(testFilePath, FileMode.OpenOrCreate, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
br.Read();
Console.WriteLine("文件被打开");
int result =FileStatus.FileIsOpen(testFilePath);
Console.WriteLine(result);
br.Close();
Console.WriteLine("文件被关闭");
result = FileStatus.FileIsOpen(testFilePath);
Console.WriteLine(result);
Console.ReadLine();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有