【发布时间】:2013-08-06 13:15:07
【问题描述】:
如何在记事本中查看是否打开了特定的.txt文件?
我已经尝试过这里提到的解决方案
Is there a way to check if a file is in use?
但它们适用于 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;
}
我在这里错过了什么吗??
我的要求: 我有类似于 VSS 的应用程序。当用户签出特定文件并打开并尝试签入相同的文件时,它已打开。假设应用程序会发出警告消息。为此,我使用了上述功能。它适用于 word 和 pdf。
【问题讨论】:
-
我相信这是因为记事本只是打开文件,读取流并关闭流,因此没有锁定文件。但是 Word 会保持文件的打开和锁定状态。
-
Belogix 是正确的,文件没有被使用,因此你的代码工作正常
-
您这样做是为了达到什么目的?正如已经评论的那样,这种方法对于任何加载/读取/关闭的应用程序都会失败
-
你的最终目标是什么?如果文件不影响文件,为什么有兴趣知道文件是否在记事本中打开?
-
不可能,唯一关闭的方法是检查所有打开的记事本的窗口标题,但它们只包含文件名,而不是路径,所以它不会真正起作用