C# 进程(robocopy)未关闭
我使用Robocopy将文件从一台计算机移动到另一台计算机。我使用的代码如下:C# 进程(robocopy)未关闭,c#,process,C#,Process,我使用Robocopy将文件从一台计算机移动到另一台计算机。我使用的代码如下: public void MoveRecords() { try { using (Process Robocopy = new Process()) { Robocopy.StartInfo.FileName = this._commandPromptCommand;
public void MoveRecords()
{
try
{
using (Process Robocopy = new Process())
{
Robocopy.StartInfo.FileName = this._commandPromptCommand;
Robocopy.StartInfo.Arguments = this._commandPromptString;
Robocopy.StartInfo.UseShellExecute = false;
Robocopy.StartInfo.CreateNoWindow = true;
Robocopy.Start();
DateTime StartTime = DateTime.Now;
if (Robocopy.WaitForExit(AppSettings.MaxMoveOperationWaitTime))
{
TimeSpan ElapsedTime = DateTime.Now - StartTime;
this._logRobocopyExitCode(Robocopy.ExitCode, ElapsedTime);
}
else
{
Logger.Write(string.Format("Timeout occured for the move operation of {0} from {1}. ", _getFilesInProgress(), this._ip), EventLogEntryType.Error);
Robocopy.Kill();
}
}
}
catch (Exception ex)
{
Logger.Write(ex, EventLogEntryType.Error);
}
}
当我查看任务管理器时,我看到许多控制台窗口主机和Microsoft Robocopy进程。您可以从下面的屏幕截图中看到情况
如何解决此问题?也许您需要一个流程。关闭。以下是我在VB.NET中使用的示例:
' Create an instance of the command prompt and run RoboCopy for the current network location.
Try
pCmdPrompt = New Process
With pCmdPrompt
.StartInfo.FileName = Environment.SystemDirectory & "\RoboCopy.exe" ' Resides in System32
.StartInfo.Arguments = strDirSrc & " " & strDirDest & strRoboCopyArg & strLogFileFullName
End With
pCmdPrompt.Start() ' Begin RoboCopy
pCmdPrompt.WaitForExit() ' Wait for RoboCopy to complete. Needed in order to obtain DateEnd for Duration details for log.
pCmdPrompt.Close() ' RoboCopy completed. Close.
pCmdPrompt = Nothing ' Clean up for next loop.
' RoboCopy occurred w/o exception. Set string that will be used for log entry.
strDetailsDone = "Success"
Catch ex As Exception
' Exception occurred during RoboCopy. Set string that will be used for log entry.
' Continue w/ processing - do NOT halt application.
strDetailsDone = "Exception occurred: " & ex.Message & vbCrLf & ex.StackTrace
If pCmdPrompt IsNot Nothing Then pCmdPrompt = Nothing
End Try
AppSettings.MaxMoveOperationWaitTime中包含什么值?250000但这不是一个重要的点!那么commandPromptCommand和commandPromptString设置为什么?RoboCopy实例是否可能正在等待用户输入?也许您应该将CreateNoWindow设置为false并观察它正在做什么。您可以将调试器连接到robocopy以查看它在等待什么。