Vij*_*ani 2 c# file-io file winforms
如何查找是否在记事本中打开特定的.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)
我在这里错过了什么.
我的要求: 我的应用程序与VSS类似.当用户签出特定文件并打开时,并在打开时尝试签入.应用程序假设要发出警告消息.为此我使用了上述功能.它适用于word和pdf.
Bel*_*gix 11
扩展我的评论.仅当应用程序保持句柄处于打开状态时,才会锁定文件.例如Word将打开文件,读入流并维护句柄,以便其他应用程序在用户处理时无法删除该文件.
记事本和其他应用程序,只需打开文件,读入整个流,然后关闭文件释放他们拥有的锁.这意味着文件不再被锁定,可以被其他应用程序编辑甚至删除,而记事本也不会关心,因为它在内存中有自己的副本.
您可以尝试使用记事本实例并检查文件是否已打开,但这最终不是一个好主意.如果文件没有锁定,那么你可以自由地用它做你想做的事情.
| 归档时间: |
|
| 查看次数: |
3531 次 |
| 最近记录: |