Issue with using DOS commands in C#

  • Thread starter Thread starter Ravichandra
  • Start date Start date
R

Ravichandra

I am using following code

string src = "C:\Source";
string dest = "C:\Destination";
copyCmdStr = "XCOPY "+ src + " "+ dest;
ProcessStartInfo processInfo = new ProcessStartInfo("CMD.Exe");
processInfo.Arguments = copyCmdStr;
processInfo.UseShellExecute = false;
processInfo.CreateNoWindow = true;
Process.Start(processInfo);

Nothing happens when this code is executed. Please help
 
Ravichandra said:
I am using following code

string src = "C:\Source";
string dest = "C:\Destination";
copyCmdStr = "XCOPY "+ src + " "+ dest;
ProcessStartInfo processInfo = new ProcessStartInfo("CMD.Exe");
processInfo.Arguments = copyCmdStr;
processInfo.UseShellExecute = false;
processInfo.CreateNoWindow = true;
Process.Start(processInfo);

Nothing happens when this code is executed. Please help

My first question is why are you using xcopy from within a C# app to copy a
file? Go with System.IO.File.Copy(src, dest); and you're done. If those
are directories, then get the files in the directory, create the target,
then copy the files over.
 
This is the first part of the code I am writing. In general, the
requirement is to support all the 20 odd switches of the Xcopy command.
 
Ravichandra said:
This is the first part of the code I am writing. In general, the
requirement is to support all the 20 odd switches of the Xcopy command.

Then simply make the two following changes:

//...
copyCmdStr = src + " "+ dest;
//...
ProcessStartInfo processInfo = new ProcessStartInfo("xcopy.exe");
//...


Or leave the processstartinfo as cmd.exe and change the copyCmdStr to "/c
xcopy.exe " + src + " "+ dest;

I would go with the former as xcopy is the real process you want to start,
not cmd.exe.
 
Back
Top