Dan*_*dia 2 .net c# cmd winforms
我现在正在给自己编写一个C#备份程序.我正在为接口使用标准的Windows窗体,并将cmd.exe作为一个新进程调用,然后在此新进程中使用XCOPY.除了我想要添加的最后一个功能外,每个东西都很好用,这就是打破操作的能力.
从本机命令提示符,我可以使用ctrl + c干净地完成此操作,但尽可能尝试,我无法使用winforms和process方法复制此功能.我已经尝试重定向standardinput并使用它来发送consolespecialkeys.ControlC到进程,我也尝试发送0x03和"/ x03",我在其他论坛帖子上读到的都是ctrl + c的十六进制代码.我发送的任何内容都没有注册,退出该进程会导致用户界面失效,但xcopy.exe会在后台运行.手动杀死xcopy.exe导致它将复制的文件保留为半复制和损坏,而不是在命令提示符中使用ctrl + c发生的事情.
我错过了一些非常明显的东西吗?我是C#的新手,所以我会举起手来承认这很可能是我很慢,或者误解了这个过程如何使用cmd.exe.但是,由于进程支持标准输入重定向,所以至少应该对我起作用.我已经将我的代码的基本轮廓放在下面,以防它有助于确定我在哪里弄乱.
string XCopyArguments = "\"" + dir.FullName + "\" \"" + destination + "\" /D /S /I /E";
Process XCopyProcess = new Process();
ProcessStartInfo XCopyStartInfo = new ProcessStartInfo();
XCopyStartInfo.FileName = "CMD.exe ";
XCopyStartInfo.RedirectStandardError = true;
XCopyStartInfo.RedirectStandardOutput = true;
XCopyStartInfo.RedirectStandardInput = true;
XCopyStartInfo.UseShellExecute = false;
XCopyStartInfo.CreateNoWindow = true;
XCopyStartInfo.Arguments = " /D /c XCOPY " + XCopyArguments;
XCopyProcess.EnableRaisingEvents = true;
XCopyProcess.StartInfo = XCopyStartInfo;
XCopyProcess.Start();
XCopyProcess.WaitForExit(15000);
int ExitCode = XCopyProcess.ExitCode;
if (ExitCode > 0 & !XCopyProcess.HasExited)
{
XCopyProcess.Kill();
}
XCopyProcess.Dispose();
Run Code Online (Sandbox Code Playgroud)
非常感谢任何人提供的任何帮助.
Tor*_*gen 13
我不想成为besserwisser,但我认为你在程序中进行复制要好得多.使用System.IO命名空间中的File,Directory和其他类,它非常简单,让您完全控制报告进度,取消操作等.
| 归档时间: |
|
| 查看次数: |
9644 次 |
| 最近记录: |