C# 使用参数从cmd执行程序

C# 使用参数从cmd执行程序,c#,process,cmd,command-line-arguments,C#,Process,Cmd,Command Line Arguments,我有一个程序,需要从cmd运行,并带有参数,比如执行文件名为 program.exe 我需要使用args从cmd运行它,cmd中的整个命令如下所示: c:\ram\program.exe /path = c:\program files\NV 如您所见,路径是:“c:\ram\” 执行文件为:“program.exe” 我需要发送的参数是:/path=c:\program files\NV 我怎么做 我尝试这样打开流程: string strArguments = @"/path = c:\p

我有一个程序,需要从cmd运行,并带有参数,比如执行文件名为

program.exe

我需要使用args从cmd运行它,cmd中的整个命令如下所示:

c:\ram\program.exe /path = c:\program files\NV
如您所见,路径是:“c:\ram\”

执行文件为:“program.exe”

我需要发送的参数是:
/path=c:\program files\NV

我怎么做

我尝试这样打开流程:

string strArguments = @"/path = c:\program files\NV";
Process p = new Process();
p.StartInfo.FileName = "program.exe";
p.StartInfo.WorkingDirectory = "c:\\ram\\";
p.StartInfo.Arguments = strArguments;
p.Start();
这不好,我想问题可能是我没有从CMD访问exe文件,也许我错了…有人知道我怎么做吗

谢谢

试试这些东西

p.StartInfo.FileName=“c:\\ram\\program.exe”未设置
工作目录

而这个更可能是问题的根源

string strArguments = @"/path = ""c:\program files\NV""";
当路径中有空格时,必须用引号将整个路径括起来。完整代码如下所示

string strArguments = @"/path=""c:\program files\NV""";
Process p = new Process();
p.StartInfo.FileName = @"c:\ram\program.exe";
p.StartInfo.Arguments = strArguments;
p.Start();
它应该做你想做的事情

1.运行“cmd.exe”

2.转到这个目录:“c:\ram\”(当然在cmd中)

3.使用以下参数执行文件“program.exe”:/path=c \:program files\NV


它接受c:\ram\文件夹中的program.exe,并使用带有指定参数的cmd执行它。

代码行p.StartInfo.WorkingDirectory=“c:\ram\”;不起作用。它应该是p.StartInfo.WorkingDirectory=@“c:\ram\”;我错过了在我程序的消息中写的:p.StartInfo.WorkingDirectory=“c:\\ram\\”我面临的问题是无法让进程“program.exe”执行,我成功执行了程序我的问题是我需要将参数传递给程序,这可以通过cmd完成,正如你所看到的,我没有按照我的需要从cmd中打开exe,我如何才能从cmd中打开cmd和cmd来执行我的program.exe?如何在cmd中找到我需要的路径并传递相关参数?你的英语不太好。我不太明白你的第一句话。您正在运行一个要从中运行cmd的应用程序,并从该cmd运行以下命令:1.运行“cmd.exe”。2.转到这个目录:“c:\ram\”(当然在cmd中)。3.使用以下参数执行文件“program.exe”:/path=c\:program files\NV。如何在代码中模拟此过程?谢谢大家!@我想是的。在我的答案中尝试代码,我包含了完整的代码片段。只需将其复制到您的项目中并进行尝试。