Skip to content

Instantly share code, notes, and snippets.

@sakyaer
Last active May 25, 2020 13:56
Show Gist options
  • Save sakyaer/058c506c2cdea35cac0b6a25a89ea833 to your computer and use it in GitHub Desktop.
Save sakyaer/058c506c2cdea35cac0b6a25a89ea833 to your computer and use it in GitHub Desktop.
|-|{"files":{"CallExteralExe.csharp":{"env":"plain","abbr":""}},"tag":"C#"}
string fileName = @"c:\aaa.doc";//要检查被那个进程占用的文件
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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment