C# 中的命令行参数

发布于 2024-12-02 07:36:58 字数 476 浏览 2 评论 0原文

再次大家好 Stackoverflow 社区,

今天我尝试在 C# 中使用命令行参数执行一个应用程序,这并不像我尝试

Process.Start(foldervar + "cocacola.exe", "pepsi.txt");

Cocacola.exe 写入并登录其当前文件夹那样困难。在我的命令行中,我像这样手动编写它,

C:\myfolder>cocacola.exe pepsi.txt

效果很好,但如果我在 C# 中尝试,则完全失败。

我读到 C# 将命令解析为 C:\myfolder>cocacola pepsi.txt,没有“.EXE”结尾。而且我手动测试了一下没有结局,这行不通。

现在,我的问题是让 C# 执行它的正确方法是什么 C:\myfolder>cocacola.exe pepsi.txt 带有“.EXE”

Hello again Stackoverflow community,

Today I am trying to execute an application with commandline parameters in C#, that not realy difficult like I tried

Process.Start(foldervar + "cocacola.exe", "pepsi.txt");

Cocacola.exe writes and Log in its current folder. In my commandline I write it manually like this

C:\myfolder>cocacola.exe pepsi.txt

Works wonderful but if I try it in C# a total fail.

I read that C# parses the command as C:\myfolder>cocacola pepsi.txt, without the ".EXE" ending. And I tested it manually without the ending, and this does not work.

Now, my question is what is the correct way to get C# executing it C:\myfolder>cocacola.exe pepsi.txt with the ".EXE"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遮云壑 2024-12-09 07:36:58

使用 ProcessStartInfo

http://www.dotnetperls.com/process-start

示例:

    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.WorkingDirectory=@"c:\someplace";
    proc.StartInfo.FileName="cocacola.exe";
    proc.StartInfo.Arguments="pepsi.txt";
    proc.Start();
    proc.WaitForExit();

这里是文档StartInfo 属性:

http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx

use ProcessStartInfo

http://www.dotnetperls.com/process-start

example:

    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.WorkingDirectory=@"c:\someplace";
    proc.StartInfo.FileName="cocacola.exe";
    proc.StartInfo.Arguments="pepsi.txt";
    proc.Start();
    proc.WaitForExit();

here is docs on the StartInfo properties:

http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx

陌路黄昏 2024-12-09 07:36:58

尝试设置 StartInfo 属性。

Process process = new Process();
process.StartInfo.FileName = @"C:\myfolder\cocacola.exe";
process.StartInfo.Arguments = @"C:\myfolder\pepsi.txt";
process.Start();

Try setting the StartInfo properties.

Process process = new Process();
process.StartInfo.FileName = @"C:\myfolder\cocacola.exe";
process.StartInfo.Arguments = @"C:\myfolder\pepsi.txt";
process.Start();
伊面 2024-12-09 07:36:58

ProcessStartInfo 具有 WorkingDirectory 属性,您应将其设置为 C:\myfolder

请参阅:http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory.aspx

ProcessStartInfo has the WorkingDirectory property you should set to C:\myfolder

see: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory.aspx

通知家属抬走 2024-12-09 07:36:58

您需要先设置工作目录

string foldervar = @"C:\myfolder";
Process process = new Process();
process.StartInfo.WorkingDirectory = foldervar;
process.StartInfo.FileName = @"cocacola.exe";
process.StartInfo.Arguments = @"pepsi.txt";
process.Start();

设置工作目录相当于在运行程序之前cd进入正确的目录。这就是相对路径的相对路径。

You need to set the working directory first

string foldervar = @"C:\myfolder";
Process process = new Process();
process.StartInfo.WorkingDirectory = foldervar;
process.StartInfo.FileName = @"cocacola.exe";
process.StartInfo.Arguments = @"pepsi.txt";
process.Start();

Setting the WorkingDirectory is equivilent to cding into the proper directory before running programs. It's what relative paths are relative to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文