复制后删除目录XCOPY

时间:2019-01-02 23:06:38

标签: c# xcopy

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

 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;
            }
        }

我想知道将源目录成功复制到另一个目录后是否可以删除它。

2 个答案:

答案 0 :(得分:2)

如果要保留.Net方法,则可以在finally语句中使用Directory.Delete。第二个参数指示删除子文件夹/文件。更多详细信息here

Directory.Delete(path,true);

答案 1 :(得分:0)

您可以使用robocopy代替xcopy

robocopy from_folder to_folder files_to_copy /MOVE

xcopy需要.bat脚本才能具有与robocopy的1行相同的功能

例如:

xcopy /D /V %1 %2

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