0

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.

  • 2
    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. Use `robocopy.exe` as the executable and pass only the `robocopy` arguments to the `Arguments` property. Also make sure you quote the file names, in case they contain spaces – Panagiotis Kanavos Feb 04 '21 at 10:35
  • What doesn't work exactly? Did you try to run the same command manually? – Pavel Anikhouski Feb 04 '21 at 10:36
  • Yes when i run the command manually it's working –  Feb 04 '21 at 10:50
  • Define "doesn't work", does it throw an error? Do the files just not copy? What happens? What error are you getting? – mxmissile Feb 04 '21 at 14:39

2 Answers2

1

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

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • it's not working even with "C:/windows/system32/Robocopy.exe" instead of "robocopy" –  Feb 04 '21 at 10:48
  • @JoevinFerret first, paths on Windows use a backslash. Second, what does `not working` mean? Do you get an exception? `Start` will throw an exception if the executable can't be found. You can wait for `robocopy` to finish with `Process.WaitForExit()` and read the output from the [Process.StandardOutput](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standardoutput?view=net-5.0) property. Errors are written to [StandardError](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standarderror?view=net-5.0) – Panagiotis Kanavos Feb 04 '21 at 15:45
  • If the app throws an error, it will write it to the standard error stream – Panagiotis Kanavos Feb 04 '21 at 15:49
0

Your code works for me.

  • SOLUTION 0 (EDIT)

You should use double quotes for you paths:

string.Format("/C ROBOCOPY \"{0}\" \"{1}\" /E /MT:32", srcPath, dstPath)
  • SOLUTION 1

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";
  • SOLUTION 2

If this is not your problem, check if you have write and/or read permissions on the source and destiny paths.

  • SOLUTION 3

Check this library: RoboSharp on Github. It is also available on Nuget.

  • SOLUTION 4

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, @.

D.Kastier
  • 2,640
  • 3
  • 25
  • 40
  • Do you think it's because my source path is on a server ? I think it shouldn't be a problem because it's working when i launch it from the cmd –  Feb 04 '21 at 11:32
  • First, check the edit. Maybe the problem is the missing ``"quotes"``. About your question, it is hard to say... But if you can copy the files with the ``Windows File Explorer``, you should be able to copy with ``robocopy``. – D.Kastier Feb 04 '21 at 11:39
  • Have you tried to run your ``exe`` as ``Administrator``? – D.Kastier Feb 04 '21 at 11:40
  • Yes i know and it's work in cmd it's only in my code that it's don't want to work. –  Feb 04 '21 at 13:06
  • Try read the output from the ``Process``, [see this](https://stackoverflow.com/a/4291965/5734097) to learn how to. Maybe you get an error explaining what is going on. – D.Kastier Feb 04 '21 at 14:21
  • Also, if you are copying from the network, mind the slashes: ``"\\NAME_or_IP\\"`` is cmd is equals to ``@"\\NAME_or_IP\\"`` (in C#) but different from ``"\\NAME_or_IP\\"`` (note the ``@``) – D.Kastier Feb 04 '21 at 14:24