C#文件永远不会被锁定

时间:2018-11-12 09:49:47

标签: c# file stream

我正在用Visual Studio Code打开一个文件,例如: C:/Users/Daniel/Desktop/Code/PTest.py

在我的代码中,我想检查该文件是否打开,就像这样:

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();
    }
    return false;
}

但是每次我调用此函数时,它都会返回false,因此它从未使用过。 我在做什么错了?

0 个答案:

没有答案
相关问题