用OLEDB操作excel,如何判断excel正在被别的程序操作? 我有一个excel文件,有2个独立的C#程序都要对其进行读写操作。由于2个C#程序相互独立,无法判断对excel读写时,另一个是否也在读写excel。请教一下,一个程序如何在操作前,得知excel正被另一个程序操作呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 trycatch进入catch,并且ex.Message里有进程错误,就说明被别的进程打开了 public static bool IsFileInUse(string filePath){ if (!File.Exists(filePath)) { return false; } SafeHandle handleValue = null; try { handleValue = FileHelper.CreateFile(filePath, FileHelper.GENERIC_WRITE, 0, IntPtr.Zero, FileHelper.OPEN_EXISTING, 0, IntPtr.Zero); bool inUse = handleValue.IsInvalid; return inUse; } finally { if (handleValue != null) { handleValue.Close(); handleValue.Dispose(); handleValue = null; } }} Add[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]private static extern Microsoft.Win32.SafeHandles.SafeFileHandle CreateFile(string lpFileName, System.UInt32 dwDesiredAccess, System.UInt32 dwShareMode, IntPtr pSecurityAttributes, System.UInt32 dwCreationDisposition, System.UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile);private static readonly uint GENERIC_WRITE = 0x40000000;private static readonly uint OPEN_EXISTING = 3; 给控件设置图片 内存暴涨,求大神指导。 Datetime 小妹虚心求教SQLite数据刷新问题! ==如何实现字节数组和结构之间的相互转换?==== 存储过程 再问关于zzbird 的留言簿,数据处理看不懂,谢谢各位了! ACE.OleDb 读取Excel时 自动截取长度为255个字符 winform的treeview显示不了节点的图片是怎么回事情? 已知椭圆的长短半径,和起始角度,求圆心到该角度的坐标值 PASSWORD会是关键字吗? VS2010水晶报表打印问题 C#窗口刷新问题,求高手指教
catch进入catch,并且ex.Message里有进程错误,就说明被别的进程打开了
{
if (!File.Exists(filePath))
{
return false;
} SafeHandle handleValue = null; try
{
handleValue = FileHelper.CreateFile(filePath, FileHelper.GENERIC_WRITE, 0, IntPtr.Zero, FileHelper.OPEN_EXISTING, 0, IntPtr.Zero); bool inUse = handleValue.IsInvalid; return inUse;
}
finally
{
if (handleValue != null)
{
handleValue.Close(); handleValue.Dispose(); handleValue = null;
}
}
}
private static extern Microsoft.Win32.SafeHandles.SafeFileHandle CreateFile(string lpFileName, System.UInt32 dwDesiredAccess, System.UInt32 dwShareMode, IntPtr pSecurityAttributes, System.UInt32 dwCreationDisposition, System.UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile);private static readonly uint GENERIC_WRITE = 0x40000000;
private static readonly uint OPEN_EXISTING = 3;