-1

I need to write a small console program for copying some content to AppData/Local.

As a source, I have a folder with a space in it. Lets call it "My Folder". My folder contains subfolders and other content.

So I have ./MyFolder and I need to copy it to C:\Users\Name\AppData\Local\My Folder

Now, what I have Done:

  1. I know, that My Folder exists in ./

  2. I can access AppData, because before copying, I need to remove some old trashy My Folder created earlier.

  3. I have Administrator rights.

And now, I have this code snippet:

Process process = new Process();
process.StartInfo.FileName = "xcopy";

string stringsource = @"./My Folder";
string stringdestination = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string all = stringsource + " " + stringdestination + "  /e /h /c /I";
/*this is for testing, resulting in CORRECT string path*/
Console.WriteLine(all);
process.StartInfo.Arguments = all;
process.Start();

This is in try/catch block and results with no error. But in destination AppData/Local, there is no new My Folder.

I have also tried:

string stringsource = "./My Folder";
string all = @""+stringsource + " " + stringdestination + "  / e / h / c / I";

I have also tried:

 process.StartInfo.Arguments = @"./My Folder "+Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)+   / e / h / c / I";

I have also tried:

 process.StartInfo.Arguments = @"'./My Folder' "+Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)+   / e / h / c / I";

none of them worked. even with no error during copying, no content is actually copied. What is wrong here please?

Zorak
  • 709
  • 7
  • 24
  • What's with all the spaces after the slashes? Also, windows uses a backslash as path separator. – ths Jun 17 '16 at 20:26
  • Try adding quotes. Because you are using `@`, you do this by adding 2 quotes everywhere you want a quote to show up: `process.StartInfo.Arguments = @"""./My Folder"""+ ... `. Without `@` you escape it with `\ `: `process.StartInfo.Arguments = "\"./My Folder\""+ ... `. – Quantic Jun 17 '16 at 20:29
  • changed ./ to .\ and still not working .... Also, I missed that spaces between /e .... and again, tried both variants with ./ and .\ ... and still not working – Zorak Jun 17 '16 at 20:36
  • In DOS, you have to enclose a path in quotes if there's a space in it. – Tim Jun 17 '16 at 20:50
  • thanks to all of you, especially to @Quantic who brought me to the correct path and I was able to find final solution - it now works, solution below as answer :) – Zorak Jun 17 '16 at 20:53

1 Answers1

1

@Quantic thank you for comment, which was the key (or rather a clue or lead) for solution.

What works is:

string all = @"""./My Folder"" """ + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),"My Folder") + "\"  /e /h /c /I";

which results in string path in format that is required as input for xcopy tool

"./My Folder" "C:\Users\Name\AppData\My Folder" /e /h /c /I
Zorak
  • 709
  • 7
  • 24