复制后删除目录 XCOPY

Remove directory after copy it XCOPY

我正在使用 C#,使用 XCOPY。我有将完整目录复制到另一个目录的方法:

 public static void ProcessXcopy(string SolutionDirectory, string TargetDirectory)
        {
            // Use ProcessStartInfo class
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.CreateNoWindow = true;
            startInfo.UseShellExecute = false;

            //Give the name as Xcopy
            startInfo.FileName = "xcopy";

            //make the window Hidden
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;

            //Send the Source and destination as Arguments to the process
            startInfo.Arguments = "\"" + SolutionDirectory + "\"" + " " + "\"" + TargetDirectory + "\"" + @" /e /y /I /B";

            try
            {
                // Start the process with the info we specified.
                // Call WaitForExit and then the using statement will close.
                using (Process exeProcess = Process.Start(startInfo))
                {
                    exeProcess.WaitForExit();
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }

我想知道是否有办法在成功复制到另一个目录后删除源目录。

您可以使用 robocopy 而不是 xcopy

robocopy from_folder to_folder files_to_copy /MOVE

xcopy 需要 .bat 脚本才能与 1 行 robocopy

具有相同的功能

例如:

xcopy /D /V %1 %2

if errorlevel 0 (
    del /Q %1
    exit /B
)

如果你想坚持 .Net 方法,你可以在 finally 语句中使用 Directory.Delete。第二个参数表示移除SubFolder/Files。更多详情 here

Directory.Delete(path,true);