c# - 传递带有空格的路径作为参数
问题描述
我在调用批处理文件的 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”并停在空格处。如何获得完整路径?
解决方案
尝试这个 :
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"
推荐阅读
- r - 根据 data.table 中附加列中的条件从同一列中对多行进行子集
- javascript - 需要帮助使用 express-handlebars 遍历变量的子项
- java - 错误:在 PytorchDemoApp 中找不到符号
- python - 如何在 numpy 数组中实现高效的增量搜索?
- javascript - 调整标签内容和背景高度
- java - 用于在二维矩阵中查找相同符号簇的 DFS 函数
- xpages - xp:dataContext 变量是只读的吗?
- excel - 代码不会从某些 URL 中提取图像
- c# - 带有脚本标签元素的部分视图违反了内容安全策略
- sql - 根据参数隐藏列 - SSRS