首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >复制XCOPY后删除目录

复制XCOPY后删除目录
EN

Stack Overflow用户
提问于 2019-01-02 23:06:38
回答 2查看 871关注 0票数 1

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

代码语言:javascript
复制
 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;
            }
        }

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-02 23:11:30

如果要粘贴.Net方法,可以在finally语句中使用Directory.Delete。第二个参数指示要删除子文件夹/文件。更多细节这里

代码语言:javascript
复制
Directory.Delete(path,true);
票数 2
EN

Stack Overflow用户

发布于 2019-01-02 23:11:14

您可以使用robocopy而不是xcopy

代码语言:javascript
复制
robocopy from_folder to_folder files_to_copy /MOVE

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

例如:

代码语言:javascript
复制
xcopy /D /V %1 %2

if errorlevel 0 (
    del /Q %1
    exit /B
)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54014342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档