C# 使用多个参数启动应用程序

问题描述 投票:0回答:4

我一直在尝试从 C# 应用程序启动应用程序,但无法正常启动。从 cmd,应用程序加上参数启动一个显示输出的小窗口,然后应用程序最小化到系统托盘。

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

    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)

也不起作用。非常感谢有关如何解决此问题的任何帮助。

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

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

问候

c# process
4个回答
14
投票

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

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

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

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();
来避免。 我也用过
UseShellExecute
CreateNoWindow
。仅在这种情况下

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

使 PrintingArgs 应用程序不显示窗口(即使我只放置

p.CreateNoWindow = true
它也会显示一个窗口)。

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

 Process.Start(fileName, args);

不使用您在 Process 实例中通过 StartInfo 设置的信息。

希望这会有所帮助, 问候


6
投票

不确定是否有人还在关注这个,但这就是我的想法。

string genArgs = arg1 + " " + arg2;
string pathToFile = "Your\Path";
Process runProg = new Process();
try
{
    runProg.StartInfo.FileName = pathToFile;
    runProg.StartInfo.Arguments = genArgs;
    runProg.StartInfo.CreateNoWindow = true;
    runProg.Start();
}
catch (Exception ex)
{
    MessageBox.Show("Could not start program " + ex);
}

在字符串中添加空格允许将两个参数传递到我想要运行的程序中。执行代码后程序运行没有问题。


2
投票

您是否将 ProcessWindowStyle 设置为隐藏? 这是我的代码,工作正常:

Process p=new Process();
p.StartInfo.FileName = filePath;//filePath of the application
p.StartInfo.Arguments = launchArguments;
p.StartInfo.WindowStyle = (ProcessWindowStyle)ProcessStyle;//Set it to **Normal**
p.Start();

1
投票
      System.Diagnostics.Process.Start(FileName,args);

例如

     System.Diagnostics.Process.Start("iexplore.exe",Application.StartupPath+ "\\Test.xml");
© www.soinside.com 2019 - 2024. All rights reserved.