我在c:\my_files\test1.txt
中放置了一个文本文件。使用C#时,我需要检查文件是否处于打开状态。如果是,我需要关闭编辑器。
在下面的代码中,如果文件处于打开状态,则不会转到catch块。
string path = @"c:\my_files\test1.txt";
FileStream stream = null;
try
{
stream = File.Open( path, FileMode.Open, FileAccess.Read, 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;
Console.WriteLine("true");
}
finally
{
if (stream != null)
stream.Close();
}
发布于 2016-10-06 21:27:10
您无法确定Notepad.exe应用程序是否在内存中打开了文件-它不保留打开的文件流。您也许能够检查Notepad应用程序进程窗口标题中是否包含文件的名称,但它没有文件路径,因此这是非常脆弱的。
由于记事本没有打开文件,因此尝试关闭该文件没有任何意义。你可以尝试关闭记事本,如果你认为文件是复制到它的内存缓冲区是你认为它是…
https://stackoverflow.com/questions/39895659
复制相似问题