首页 > 解决方案 > 传递带有空格的路径作为参数

问题描述

我在调用批处理文件的 C# 中运行一些代码,我需要传递一个带有空格的路径作为参数,但它不起作用。

我试图在批处理 %1、%~1、“%1”、“%1”中以不同的方式调用我的论点。这些都不起作用。同样在我的 C# 代码中,我尝试将其转换为字符串,但它也不起作用

C# 代码。

string argument =   textBox10.Text.ToString()  ; 
string command = @"/c powershell -executionpolicy unrestricted X:\PathToBatch\Run.bat" + " " + argument;
System.Diagnostics.Process.Start("cmd.exe", command);

批号 :

echo %1
Pause

当我将参数 C:\Program Files\Test 作为目录传递时,它会打印“C:\Program”并停在空格处。如何获得完整路径?

标签: c#batch-file

解决方案


尝试这个 :

string command = @"/c powershell -executionpolicy unrestricted X:\PathToBatch\Run.bat"+ " \""+ argument +"\" ";
System.Diagnostics.Process.Start("cmd.exe", command);

这将在您的控制台中“写入”以下行:

/c powershell -executionpolicy unrestricted X:\PathToBatch\Run.bat "C:\Program Files\Test"

推荐阅读