c# - Process.StartInfo.Arguments - 带有 space 的目录的路径

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

我需要编写一个小的控制台程序来将一些内容复制到 AppData/Local。

作为来源,我有一个文件夹,里面有一个 space。让我们称之为 "My Folder"。我的文件夹包含子文件夹和其他内容。

所以我有 ./MyFolder,我需要将它复制到 C:\Users\Name\AppData\Local\My 文件夹

现在,我做了什么:

  1. 我知道,我的文件夹存在于./

  2. 我可以访问AppData,因为在复制之前,我需要删除一些 较早创建的旧垃圾我的文件夹。

  3. 我有管理员权限。

现在,我有了这个代码片段:

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();

这在 try/catch 块中并且结果没有错误。但是在目标 AppData/Local 中,没有新的我的文件夹。

我也试过:

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

我也试过:

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

我也试过:

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

none 他们成功了。即使在复制过程中没有错误,实际上也没有复制任何内容。请问这里有什么问题吗?

@Quantic 感谢您的评论,这是解决问题的关键(或者更确切地说是线索或线索)。

有效的是:

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

这会导致字符串路径采用 xcopy 工具输入所需的格式

"./My Folder" "C:\Users\Name\AppData\My Folder" /e /h /c /I