C# Launch application with multiple arguments

后端 未结 4 1979
执笔经年
执笔经年 2020-12-15 09:24

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 showin

相关标签:
4条回答
  • 2020-12-15 09:59
          System.Diagnostics.Process.Start(FileName,args);
    

    Eg

         System.Diagnostics.Process.Start("iexplore.exe",Application.StartupPath+ "\\Test.xml");
    
    0 讨论(0)
  • 2020-12-15 10:07

    Have u set your ProcessWindowStyle to Hidden? This is my code, working fine:

    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();
    
    0 讨论(0)
  • 2020-12-15 10:17

    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
    }
    

    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();
    

    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;
    

    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);
    

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

    Hope this will help, regards

    0 讨论(0)
  • 2020-12-15 10:20

    Not sure if anyone is still following this but here is what I came up with.

    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);
    }
    

    Adding a space in the string allowed two arguments to be passed into the program I wanted to run. The program ran without issue after executing the code.

    0 讨论(0)
提交回复
热议问题