我有我正在打开并在 dist 上创建文件并订阅此过程的过程ProcessExitedEvent
:
public int InvokeProcess(WiresharkProcesses process, string args)
{
try
{
string processToInvoke = null;
ProcessStartInfo startInfo = new ProcessStartInfo();
Process pros = new Process();
startInfo .FileName = processToInvoke;
startInfo .RedirectStandardOutput = true;
startInfo .RedirectStandardError = true;
startInfo .RedirectStandardInput = true;
startInfo .UseShellExecute = false;
startInfo .CreateNoWindow = true;
startInfo .Arguments = args;
pros.StartInfo = startInfo ;
pros.ErrorDataReceived += pros_ErrorDataReceived;
pros.OutputDataReceived += pros_OutputDataReceived;
pros.Exited += (object sender, EventArgs e) =>
{
if (ProcessExitedEvent != null)
ProcessExitedEvent(pros.Id);
};
pros.EnableRaisingEvents = true;
pros.Start();
pros.BeginOutputReadLine();
pros.BeginErrorReadLine();
return pros.Id;
}
catch (Exception)
{
return -1;
}
}
private void ProcessExitedEvent(int processId)
{
// Copy file
// Delete file
}
此过程在磁盘上创建文件,在ProcessExitedEvent
事件触发后我想将此文件复制到另一个位置,而不是删除旧文件,但是虽然在进程退出后触发事件,但我的代码未能删除旧文件,因为文件仍在使用所以我想确保我的进程通过使用using block
所以我需要把这个块放在哪里?