2016-12-26 29 views
0

我在尝试使用.mkv文件(vlc.exe,mpc-hc.exe等)进程的名称如何找出正在使用哪个进程的文件

我已经设法发现它是否在使用中,但我想知道至关重要的进程正在使用该文件。

我试过了这个帖子How do I find out which process is locking a file using .NET?的建议,但只是插入文件的路径并运行它似乎没有工作,它没有显示任何进程(我可以运行代码没有错误)。

public static bool IsFileinUse(FileInfo file) 
{ 
    FileStream stream = null; 

    try 
    { 
     stream = file.Open(FileMode.Open, FileAccess.ReadWrite, 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; 
    } 
    finally 
    { 
     if (stream != null) 
      stream.Close(); 
    } 
    return false; 
} 
+0

这可以帮助你在正确的方向走http://stackoverflow.com/questions/958123/powershell-script-to-check-an-application-这就是锁定文件 –

+0

http://stackoverflow.com/questions/317071/how-do-i-find-out-which-process-is-locking-a-file-using-net –

+1

http:// stackoverflow.com/questions/860656/using-c-how-does-one-figure-out-what-process-locked-a-file –

回答

-1

试试这个:

string fileName = @"c:\yourFile.doc";//Path to locked file 

Process tool = new Process(); 
tool.StartInfo.FileName = "handle.exe"; 
tool.StartInfo.Arguments = fileName+" /accepteula"; 
tool.StartInfo.UseShellExecute = false; 
tool.StartInfo.RedirectStandardOutput = true; 
tool.Start();   
tool.WaitForExit(); 
string outputTool = tool.StandardOutput.ReadToEnd(); 

string matchPattern = @"(?<=\s+pid:\s+)\b(\d+)\b(?=\s+)"; 
foreach(Match match in Regex.Matches(outputTool, matchPattern)) 
{ 
    Process.GetProcessById(int.Parse(match.Value)).Kill(); //kill the process if you want 
} 
+0

什么是handle.exe? – john

+0

手柄是一个实用程序,显示有关系统中任何进程的打开手柄的信息。您可以使用它来查看打开文件的程序,或查看程序所有句柄的对象类型和名称。 – NicoRiff

+0

它是sysinternals工具的一部分...你可以从这里下载:https://download.sysinternals.com/files/Handle.zip – NicoRiff

相关问题