Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Issue using Process.Start() to execute command line statement

Status
Not open for further replies.

ashishraj14

Programmer
Mar 1, 2005
92
0
0
AU
I am having issues copying files from network drive to local machine using xcopy. The following code works alright for a console application, but as soon as i call this code in Form Load event of windows application it does not work. I am confused here, it copies files in a console application, but not windows application!

Code:
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "xcopy";

info.Arguments = string.Format("\"{0}\" \"{1}\" /r/e/c/i/y/d",
@"\\server\Templates\.",
@"C:\temp\Test Images");

info.UseShellExecute = false;
info.RedirectStandardOutput = true;
Process process = Process.Start(info);

string result = process.StandardOutput.ReadToEnd();

process.WaitForExit();

Console.WriteLine(result);
//MessageBox.Show(result);

Please assist. Thanks

 
maybe try running cmd as your process and passing in xcopy +arguments as the arguments.

You may have more luck.

You can also turn shellexecute back on and ignore redirecting the output. That way you can see what happens before trying to capture the output in your app.

 
Hi JurkMonkey

I tried running cmd as process but that didn't help. I can't ignore redirecting the output because as I have to log the outcome of the xcopy process to the log file. Is there any other way to achieve that?

Thanks

 
Redirecting the standard output for that is fine. I was just saying don't do that right away until you get it working on its own. When you don't redirect standard output all the text is written to the console window. Once you have the copy working in that window, then redirect the standardoutput and log it as needed.

can you provide more details on what's failing?

 
Someone mentioned a quirk of xcopy.exe on one of the MSDN forums. When we redirect output we have to redirect input too. If we don’t, it immediately and silently quits right after startup. I don’t know concrete reason for it, but does the trick.

Thanks everyone for help.
 
My only other thought to this was - why are you using xcopy in the first place?

Why wouldn't you just use the built in File and Directory classes in the System.IO namespace to do your move/copy?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top