If you want to execute your command in the cmd.exe
window, you can do it like this.
var process = new Process();
var startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = @"/K xcopy c:\A\*.* c:\b"
};
process.StartInfo = startInfo;
process.Start();
Note that /K
keeps the command line window open, replace it with /C
to automatically close it after copying.
If you want to start xcopy
without showing the console window and collecting the output to show it where you want, use this.
var process = new Process();
var startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "xcopy",
Arguments = @"c:\A\*.* c:\b",
RedirectStandardOutput = true
};
process.StartInfo = startInfo;
process.Start();
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
// Print the output to Standard Out
Console.WriteLine(output);
// Print the ouput to e.g. Visual Studio debug window
Debug.WriteLine(output);
Note that this only works as expected, if your B
folder exists folder does not already contain any of your files. Otherwise, the window will stay open, because you are asked, whether the directory should be created and the files should be overwritten. To not overcomplicate this by writing input, you could use the following arguments for xcopy
.
Arguments = @"c:\A\*.* c:\b\ /Y /I"
The /Y
switch will overwrite files without asking and /I
will create a non-existing directory. It is mandatory for this to work to have a trailing backslash on your destination directory path (c:\b\ instead of c:\b).