1

我使用 Robocopy 将文件从一台计算机移动到另一台计算机。我正在使用下面的代码:

    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”进程。您可以从下面的屏幕截图中看到情况。

在此处输入图像描述

我怎么解决这个问题?

4

1 回答 1

0

也许你需要一个“process.close”。以下是我在 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
于 2013-06-05T21:29:04.233 回答