本文介绍了有没有一种方法,如果一个Excel文件被打开以编程方式检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我要检查一个特定的Excel文件是否已打开。否则,当我在C#程序重新打开相同的文件,则只读格式打开。有没有办法找出该文件是否已经打开?

I want to check whether a particular Excel file is already opened. Otherwise when I reopen same file in my C# program it is opening in read only format. Is there any way to find out if the file is already open?

推荐答案

如果一旦打开另一个程序的代码可以在文件帮助你看着办吧,但你不能打开它。

If the file if opened by another program this code can help you figure it out, but you won't be able to open it

<!-- language: c# -->
protected virtual bool IsFileLocked(FileInfo file)
{
FileStream stream = null;

    try
    {
        stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
    }
    catch (IOException)
    {
        //the file is unavailable because it is:
        //still being written to
        //or being processed by another thread
        //or does not exist (has already been processed)
        return true;
    }
    finally
    {
        if (stream != null)
            stream.Close();
    }

    //file is not locked
    return false;
}



(但你不能用它做任何事情时,必须关闭文件从程序,从而拉开吧)

(BUt you can't do anything with it, the file must be closed from the program, which opened it)

这篇关于有没有一种方法,如果一个Excel文件被打开以编程方式检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:54