/// <summary>
/// 获取指定文件或目录中存在的(关联的)运行进程信息,以便后面可以解除占用
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
private
Dictionary<
int
,
string
> GetRunProcessInfos(
string
filePath)
{
Dictionary<
int
,
string
> runProcInfos =
new
Dictionary<
int
,
string
>();
string
fileName = Path.GetFileName(filePath);
var
fileRunProcs = Process.GetProcessesByName(fileName);
if
(fileRunProcs !=
null
&& fileRunProcs.Count() > 0)
{
runProcInfos = fileRunProcs.ToDictionary(p => p.Id, p => p.ProcessName);
return
runProcInfos;
}
string
fileDirName = Path.GetDirectoryName(filePath);
Process startProcess =
new
Process();
startProcess.StartInfo.FileName = RelaseAndGetHandleExePath();
startProcess.StartInfo.Arguments =
string
.Format(
"\"{0}\""
, fileDirName);
startProcess.StartInfo.UseShellExecute =
false
;
startProcess.StartInfo.RedirectStandardInput =
false
;
startProcess.StartInfo.RedirectStandardOutput =
true
;
startProcess.StartInfo.CreateNoWindow =
true
;
startProcess.StartInfo.StandardOutputEncoding = ASCIIEncoding.UTF8;
startProcess.OutputDataReceived += (sender, e) =>
{
if
(!
string
.IsNullOrEmpty(e.Data) && e.Data.IndexOf(
"pid:"
, StringComparison.OrdinalIgnoreCase) > 0)
{
var
regex =
new
System.Text.RegularExpressions.Regex(
@"(^.+(?=pid:))\bpid:\s+(\d+)\s+"
, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if
(regex.IsMatch(e.Data))
{
var
mathedResult = regex.Match(e.Data);
int
procId =
int
.Parse(mathedResult.Groups[2].Value);
string
procFileName = mathedResult.Groups[1].Value.Trim();
if
(
"explorer.exe"
.Equals(procFileName, StringComparison.OrdinalIgnoreCase))
{
return
;
}
var
regex2 =
new
System.Text.RegularExpressions.Regex(
@"\b\w{1}:.+$"
, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string
procFilePath = (regex2.Match(e.Data).Value ??
""
).Trim();
if
(filePath.Equals(procFilePath, StringComparison.OrdinalIgnoreCase) || filePath.Equals(PathJoin(procFilePath, procFileName), StringComparison.OrdinalIgnoreCase))
{
runProcInfos[procId] = procFileName;
}
else
{
if
(procFilePath.Contains(
"?"
) || procFileName.Contains(
"?"
))
{
var
regex3 =
new
System.Text.RegularExpressions.Regex(procFilePath.Replace(
@"\"
,
@"\\"
).Replace(".
", @"
\.
").Replace("
?
", "
.{1}"), System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if
(regex3.IsMatch(filePath))
{
runProcInfos[procId] = procFileName;
}
else
{
string
tempProcFilePath = PathJoin(procFilePath, procFileName);
regex3 =
new
System.Text.RegularExpressions.Regex(tempProcFilePath.Replace(
@"\"
,
@"\\"
).Replace(".
", @"
\.
").Replace("
?
", "
.{1}"), System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if
(regex3.IsMatch(filePath))
{
runProcInfos[procId] = procFileName;
}
}
}
else
if
(procFilePath.Length == filePath.Length || PathJoin(procFilePath, procFileName).Length == filePath.Length)
{
if
(MessageBox.Show(
string
.Format(
"发现文件:{0}可能被一个进程({1})占用,\n您是否需要强制终止该进程?"
, filePath, procFileName),
"发现疑似被占用进程"
, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
runProcInfos[procId] = procFileName;
}
}
}
}
}
};
startProcess.Start();
startProcess.BeginOutputReadLine();
startProcess.WaitForExit();
return
runProcInfos;
}