【问题标题】:Remove directory after copy it XCOPY复制后删除目录 XCOPY
【发布时间】:2019-01-02 23:06:38
【问题描述】:

我正在使用 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;
            }
        }

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

【问题讨论】:

  • XCopy 用于从命令行使用。为什么要使用它而不是直接从代码中执行此操作?

标签: c# xcopy


【解决方案1】:

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

Directory.Delete(path,true);

【讨论】:

    【解决方案2】:

    您可以使用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
    )
    

    【讨论】:

      猜你喜欢
      • 2014-07-14
      • 1970-01-01
      • 2017-01-28
      • 2012-06-27
      • 1970-01-01
      • 1970-01-01
      • 2012-04-19
      • 2021-03-03
      • 2010-10-09
      相关资源
      最近更新 更多