I want to run a Robocopy command from my code but it dosn't want to run.
Here's my code:
ProcessStartInfo startInfo = new ProcessStartInfo("CMD.EXE");
startInfo.Arguments = string.Format("/C ROBOCOPY {0} {1} /E /MT:32", srcPath, dstPath);
Process.Start(startInfo);
I tried this:
Process.Start("CMD.EXE", string.Format("/C ROBOCOPY {0} {1} /E /MT:32", srcPath, dstPath));
But it also dosn't work.
I don't know why it only run the cmd with no arguments but when i copy/paste my command on the cmd it work.
I have seen other topic talk about this but i none of them i found a fine answer.
The application is robocopy.exe
not cmd
. Instead of launching robocopy, you're launching a console and tell it to launch robocopy
with injected arguments. It's VERY easy to create an invalid argument string this way, especially if the file names contain spaces.
Use robocopy.exe
as the executable and pass only the robocopy
arguments to the Arguments
property. You have to ensure the path arguments are quoted, to take care of paths with spaces, eg:
ProcessStartInfo startInfo = new ProcessStartInfo("robocopy");
startInfo.Arguments = string.Format("\"{0}\" \"{1}\" /E /MT:32", srcPath, dstPath);
Process.Start(startInfo);
or
Process.Start("robocopy", string.Format("\"{0}\" \"{1}\" /E /MT:32", srcPath, dstPath));
This will work if robocopy
is in the user's PATH. If not, you'll have to pass the full path to the executable
Your code works for me.
You should use double quotes for you paths:
string.Format("/C ROBOCOPY \"{0}\" \"{1}\" /E /MT:32", srcPath, dstPath)
So I will guess that the problem is srcPath
and dstPath
. You must use the absolute path!
For example:
srcPath = @"C:\source";
dstPath = @"C:\destiny";
If this is not your problem, check if you have write
and/or read
permissions on the source and destiny paths.
Check this library: RoboSharp on Github . It is also available on Nuget .
Since you are copying from NETWORK, and the code works in the cmd
, maybe the problem is the slashes .
This: srcPath = "\\127.0.0.1\\";
is different of: srcPath = @"\\127.0.0.1\\";
.
See this link to learn more about the at sign, @ .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.