C# 使用多个参数启动应用程序 [英] C# Launch application with multiple arguments

查看:45
本文介绍了C# 使用多个参数启动应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试从 C# 应用程序启动应用程序,但未能正常启动.从 cmd 应用程序和参数启动一个小窗口,显示输出,然后应用程序最小化到系统托盘.

I have been trying to start an application from a C# application but it fails to start properly. From the cmd the application plus the arguments launch a small window showing the output then the application in minimized to the system tray.

使用下面的代码从 C# 应用程序启动应用程序会导致进程出现在任务管理器中,但没有其他内容,没有输出窗口,没有系统托盘图标.可能是什么问题?

Launching the application from the C# application using the code below results in the process appearing in the task manager but nothing else, no output window, no system tray icon. What could be the issue?

    myProcess.StartInfo.FileName = ...;
    myProcess.StartInfo.Arguments = ...;
    myProcess.Start();

还尝试传递以下内容

    myProcess.StartInfo.RedirectStandardOutput = true; //tried both
    myProcess.StartInfo.UseShellExecute = false; //tried both 
    myProcess.StartInfo.CreateNoWindow = false;

使用

    Process.Start(Filename, args)

也没有用.非常感谢有关如何解决此问题的任何帮助.

also did not work. Would really appreciate any help on how to tackle this.

更新:我认为问题可能是要传递给进程的多个参数

UPDATE: I think the issue maybe the multiple arguments that are to be passed to the process

RunMode=Server;CompanyDataBase=dbname;UserName=user;PassWord=passwd;DbUserName=dbu;Server=localhost;LanguageCode=9

问候

推荐答案

我看不出你的代码有任何错误.我写了一个小程序,将它的参数打印到控制台

I don't see any mistake in your code. I have written a little program that prints out its args to the console

static void Main (string[] args)
{
     foreach (string s in args)
         Console.WriteLine(s);
     Console.Read(); // Just to see the output
}

然后我把它放在 C: 中,作为应用程序的名称PrintingArgs.exe",所以我写了另一个执行第一个的:

and then I have put it in C:, being the name of the app "PrintingArgs.exe", so I have written another one that executes the first:

Process p = new Process();
p.StartInfo.FileName = "C:\PrintingArgs.exe";
p.StartInfo.Arguments = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18";
p.Start();

这给了我想要的数字列表输出.调用 PrintingArgs 的应用程序在到达 p.Start() 时退出,这可以通过使用 p.WaitForExit(); 或仅使用 Console.Read(); 来避免.我还使用了 p.UseShellExecutep.CreateNoWindow.只有在这种情况下

this gives me the desired output of the list of numbers. The app that calls PrintingArgs exits as it reachs p.Start(), this could be avoided by using p.WaitForExit(); or just Console.Read();. Also I have used both p.UseShellExecute and p.CreateNoWindow. Only in the case that

p.UseShellExecute = false;
p.CreateNoWindow = true;

使 PrintingArgs 应用程序不显示窗口(即使我只输入 p.CreateNoWindow = true 它显示一个窗口).

makes the PrintingArgs app not to show a window (even if I put only p.CreateNoWindow = true it shows a window).

现在我想到,也许您以错误的方式传递参数并导致其他程序失败并立即关闭,或者您没有指向正确的文件.检查路径和名称,以找出可以省略的任何错误.另外,使用

Now it comes to my mind that maybe your are passing the args in a wrong way and makes the other program to fail and close inmediately, or maybe you are not pointing to the right file. Check paths and names, in order to find any mistake you could omit. Also, using

 Process.Start(fileName, args);

不会将您通过 StartInfo 设置的信息用于 Process 实例.

does not uses the info you set up with StartInfo into your Process instance.

希望这会有所帮助,问候

Hope this will help, regards

这篇关于C# 使用多个参数启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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