如何查找是否在记事本中打开特定的.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
Belogix是正确的,该文件没有被使用,因此你的代码工作正常 – x4rf41
所以任何工作? –