Search code examples
c#xcopy

c# - Process.StartInfo.Arguments - path with dir with space


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?


Solution

  • @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