Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,
I would like to run an external program from C#. I have the source code for the program. Basically it would need to read a parameters text or XML file and run the application.
Any suggestions would be appreciated.

For example:

However I don't know how to read in a parameters file for the .exe?

C#
class Program
{
    static void Main()
    {
    LaunchCommandLineApp();
    }

    /// <summary>
    /// Launch the legacy application with some options set.
    /// </summary>
    static void LaunchCommandLineApp()
    {
    // For the example
    const string ex1 = "C:\\";
    const string ex2 = "C:\\Dir";

    // Use ProcessStartInfo class
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = false;
    startInfo.UseShellExecute = false;
    startInfo.FileName = "dcm2jpg.exe";
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;

    try
    {
        // Start the process with the info we specified.
        // Call WaitForExit and then the using statement will close.
        using (Process exeProcess = Process.Start(startInfo))
        {
        exeProcess.WaitForExit();
        }
    }
    catch
    {
        // Log error.
    }
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 26-Mar-13 13:15pm    
If your application uses the console and all data is written in the console, you can redirect the stream(s). If you really need it, you can find it all in the Process documentation, or I can help you.
But better use the alternative approach; please see my answer.
—SA

1 solution

If you have the source code for the application you want to start, do yourself a great favor: re-build it into a class library to be used in-process. All you would need it to give a public access modifier to the types and their members you would need to use in the referencing assemblies. (And you can also remove unused entry point, "Main", but this is optional.)

Starting an application is a very limiting technique, just because processes are well isolated from each other, so it's hard to control them, especially if they are not simple console-only applications.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900