使用的Process.Start与路径参数和空间 [英] Use Process.Start with parameters AND spaces in path

查看:327
本文介绍了使用的Process.Start与路径参数和空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过类似的例子,但无法找到酷似我的问题。

I've seen similar examples, but can't find something exactly like my problem.

我要像这样从C#运行命令:

I need to run a command like this from C#:

C:\FOLDER\folder with spaces\OTHER_FOLDER\executable.exe p1=hardCodedv1 p2=v2

我在运行时设定V2,所以我需要能够调用的Process.Start之前修改C#中的字符串。有谁知道如何处理这一点,因为我有我的参数之间的空间?

I'm setting v2 at runtime, so I need to be able to modify the string in C# before calling Process.Start. Does anyone know how to handle this, since I have spaces between my parameters?

推荐答案

您可以使用ProcessStartInfo类分开你的论点,文件名,工作目录和论据无后顾之忧的空格

You can use the ProcessStartInfo class to separate your arguments, FileName, WorkingDirectory and arguments without worry for spaces

string fullPath = @"C:\FOLDER\folder with spaces\OTHER_FOLDER\executable.exe"
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = Path.GetFileName(fullPath);
psi.WorkingDirectory = Path.GetDirectoryName(fullPath);
psi.Arguments = "p1=hardCodedv1 p2=" + MakeParameter();
Process.Start(psi);

,其中MakeParameter是用于p2的参数,用于返回字符串的函数

where MakeParameter is a function that returns the string to be used for the p2 parameter

这篇关于使用的Process.Start与路径参数和空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆