小编Vij*_*ani的帖子

检查是否在记事本中打开了文本文件

如何查找是否在记事本中打开特定的.txt文件?

我试过这里提到的解决方案

有没有办法检查文件是否正在使用?

但它们适用于Word和pdf文件,但不适用于在记事本中打开的txt文件.

这是我写的代码.

public bool IsFileOpen(string strFileName)
{
    bool retVal = false;
    try
    {
        if (File.Exists(pstrFileName))
        {
            using (FileStream stream = File.OpenWrite(pstrFileName))
            {
                try
                {    
                }
                catch (IOException)
                {
                    retVal = true;
                }
                finally
                {
                    stream.Close();
                    stream.Dispose();
                }
            }
        }
    }
    catch (IOException)
    { //file is opened at another location 
        retVal = true;
    }
    catch (UnauthorizedAccessException)
    { //Bypass this exception since this is due to the file is being set to read-only 
    }
    return retVal;
} 
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么.

我的要求: …

c# file-io file winforms

2
推荐指数
1
解决办法
3531
查看次数

标签 统计

c# ×1

file ×1

file-io ×1

winforms ×1