路径中 space 的问题,参数 C# 启动 python 脚本
Problem with space in path, arguments C# to launch python script
我不知道如何编写字符串格式以保持 space 的完整路径,因为它在每个 space 处被拆分。
4 个参数:
- C:/MyDoc/Example/MyCSV 文件 1.csv
- -1
- 20-05-2019
- 7
python.run_cmd("C:/MyCode.py", "C:/MyDoc/Example/MyCSV File 1.csv -1 20-05-2019 7");
函数:
public static void run_cmd(string cmd, string args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "C:/PRGM/python.exe";
start.Arguments = string.Format("{0} {1}",cmd, args);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
}
如何将文件名用引号引起来,您需要使用反斜杠对它们进行转义 \
:
python.run_cmd("C:/MyCode.py", "\"C:/MyDoc/Example/MyCSV File 1.csv\" -1 20-05-2019 7");
或使用 verbatim string:
python.run_cmd("C:/MyCode.py", @"""C:/MyDoc/Example/MyCSV File 1.csv"" -1 20-05-2019 7");
我不知道如何编写字符串格式以保持 space 的完整路径,因为它在每个 space 处被拆分。 4 个参数:
- C:/MyDoc/Example/MyCSV 文件 1.csv
- -1
- 20-05-2019
- 7
python.run_cmd("C:/MyCode.py", "C:/MyDoc/Example/MyCSV File 1.csv -1 20-05-2019 7");
函数:
public static void run_cmd(string cmd, string args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "C:/PRGM/python.exe";
start.Arguments = string.Format("{0} {1}",cmd, args);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
}
如何将文件名用引号引起来,您需要使用反斜杠对它们进行转义 \
:
python.run_cmd("C:/MyCode.py", "\"C:/MyDoc/Example/MyCSV File 1.csv\" -1 20-05-2019 7");
或使用 verbatim string:
python.run_cmd("C:/MyCode.py", @"""C:/MyDoc/Example/MyCSV File 1.csv"" -1 20-05-2019 7");