Home All Groups Group Topic Archive Search About

Issue with using DOS commands in C#

Author
21 Dec 2006 4:23 PM
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

Author
21 Dec 2006 4:47 PM
Tom Porterfield
Ravichandra wrote:
> 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.
--
Tom Porterfield
Are all your drivers up to date? click for free checkup

Author
21 Dec 2006 5:01 PM
Ravichandra
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.

Tom Porterfield wrote:
Show quoteHide quote
> Ravichandra wrote:
> > 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.
> --
> Tom Porterfield
Author
21 Dec 2006 6:06 PM
Tom Porterfield
Ravichandra wrote:
> 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.
--
Tom Porterfield



Post Thread options