如何将路径作为参数传递给 Process.Start

How to pass a path as an argument to Process.Start

下面的代码说明了一切。当我尝试将本地文件夹 xcopy 到网络共享时,路径变得一团糟,CMD 无法正确插入反斜杠。欢迎任何建议,我已经尝试了我在网上找到的所有东西(为此卡了将近 2 小时):

string command = "xcopy /s " + @"C:\Users\Me\Desktop\TempExtract\MyApp\*.* " + @"\TestShare\SharedFolder\Applications\ /Y /I";
Process Processo = new Process();
ProcessStartInfo Xcopy = new ProcessStartInfo("cmd.exe");
Xcopy.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
Xcopy.Arguments = command;
Processo = Process.Start(Xcopy);
Processo.WaitForExit();

这里的问题是 "Arguments" 无法将“\\”解码为“\”,所以我的路径如下:

C:\Users\Me\Desktop\TempExtract\MyApp\*.*

并且 CMD 无法插入双反斜杠 :( 求助!

我认为问题在于您设置的方式 ProcessStartInfo。所以 command 应该是:

string command = @"C:\Users\Me\Desktop\TempExtract\MyApp\*.* " + @"\TestShare\SharedFolder\Applications\ /Y /I";

并添加

Xcopy.FileName = "xcopy";

这对我有用:

using System.Diagnostics;

class Program
{
    static void Main(string[] args)
    {
        var command = @"C:\Users\Me\Desktop\TempExtract\MyApp\*.* " + @"\TestShare\SharedFolder\Applications\ /Y /I";
        var Processo = new Process();
        var Xcopy = new ProcessStartInfo("cmd.exe")
        {
            Arguments = command,
            FileName = "xcopy",
            UseShellExecute = false
        };
        Processo = Process.Start(Xcopy);
        Processo.WaitForExit();
    }
}

尝试在开头或命令字符串中添加/c(因为cmd.exe通过/c参数启动以下命令,查看cmd.exe的帮助)。 如果这不起作用,请在您的路径周围添加 \"