You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#调用powershell.exe传递含空格参数的问题求助

C#调用powershell.exe传递含空格参数的问题求助

我最近碰到一个棘手的问题,想在C#里启动PowerShell进程,让它再启动另一个需要带空格参数(比如"M=a b")的程序,试了各种字符串写法都没搞定,实在没办法了来求助!

首先说下,直接在PowerShell里执行下面的命令是完全正常的,程序能正确识别带空格的参数:

Start-Process -FilePath 'C:\MY.exe' -ArgumentList 'A','"M=a b"'

但我在C#里这么写就出问题了,MY.exe会报错说"M=a"找不到:

string arg = @"Start-Process -FilePath 'C:\MY.exe' -ArgumentList 'A','""M=a b""'";

psTry("powershell.exe", arg);

static void psTry(string file, string arg)
{
    Process process = new Process();
    process.StartInfo.FileName = file;
    process.StartInfo.Arguments = arg;
    process.Start();
}

我试过各种单引号、双引号的组合,不管是在PowerShell里预演不同的写法,还是在C#里调整字符串的拼接方式,比如像下面这样拆分拼接:

arg = @"...".ToString() + @"'""M=a b""'".ToString();

但还是没能解决参数传递的问题,有没有大佬能帮我看看到底哪里出错了?

备注:内容来源于stack exchange,提问作者Olav Zietlow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 12:59:33