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

如何运行带引号参数的CMD文件?C#实现遇阻求助

问题解决方法

核心原因

当传入的命令参数包含双引号时,批处理脚本中的%1会原样保留引号,导致copy命令执行时源路径被包裹在双引号中,进而触发执行异常。

方案一:修改批处理脚本

将SFTPPutProd.cmd中的代码替换为:

copy %~1 C:\Export\copy_destination

%~1是批处理的特殊语法,会自动去除参数两端的双引号,无论传入的参数是否带引号,都能正确解析出目标路径。

方案二:优化C#调用逻辑

避免手动拼接带引号的命令字符串,直接将批处理路径和参数分开传入ProcessStartInfo,让系统自动处理空格与引号问题:

private string RunCmdAndWait(string cmdPath, string arguments) {
    string result;
    try {
        ProcessStartInfo procStartInfo = new ProcessStartInfo(cmdPath, arguments) {
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        using (Process process = new Process()) {
            process.StartInfo = procStartInfo;
            process.Start();
            process.WaitForExit();
            result = process.StandardOutput.ReadToEnd();
        }
    }
    catch (Exception ex) {
        _log.WriteWarning($"执行命令文件出错:{cmdPath} {arguments}");
        result = "Error";
    }
    return result;
}

调用时直接传入路径和参数即可:

RunCmdAndWait(@"C:\Export\SFTPPutProd.cmd", @"C:\Export\*.csv");

内容的提问来源于stack exchange,提问作者Rich Bianco

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 18:23:00