判断是否打开的函数如下:
public bool IsFileOpened(string file)
{
bool result = false;
try
{
FileStream fs = File.OpenWrite(file);
fs.Close();
}
catch (Exception e)
{
result = true;
}
return result;
}
得到的结果如下:
如果文件是.txt .log .dat 则文件是否打开都显示 false;
如果文件是.doc .xls .csv 则文件打开显示true, 文件没有打开则显示false;
调用这个函数的语句为:
bool b = IsFileOpened(@"D:\新建 Microsoft Excel 工作表.xls");
if (b==true) {
MessageBox.Show("当前文件被打开,请关闭后再导入");
return;
}