Problem. You must launch another EXE using Process.Start in the C# language. The user needs to view documents and web pages, and run external programs and command line utilities. Solution. Here we see how you can use System.Diagnostics and this document has many tips on using the Process class in the C# programming language.
Use the Process.Start method to start external EXEs.
This method is in the System.Diagnostics namespace.
It will show a console window.Here we add the System.Diagnostics namespace. Our sample project is a C# console application, but these steps will work in many configurations. Get Visual Studio set up on your source file. This example sends the C:\ directory name to the operating system, which will result in Explorer opening the folder on your desktop.
=== Program that opens directory (C#) ===
using System.Diagnostics;
class Program
{
static void Main()
{
//
// Use Process.Start here.
//
Process.Start("C:\\");
}
}Here you can specify the file name alone. In this example on my system, Microsoft Word 2007 opens the file example.txt, because on the system Microsoft Word is the default .txt editor.
=== Program that opens text file (C#) ===
using System.Diagnostics;
class Program
{
static void Main()
{
//
// Open the file "example.txt" that is in the same directory as
// your .exe file you are running.
//
Process.Start("example.txt");
}
}You can specify the URL and send it to Windows. Here we launch a Google search for some search terms, such as "VB.NET awesome". Your users might run Firefox, Google Chrome, Internet Explorer, Opera, or Safari on Windows. Call the following code with SearchGoogle("VB.NET awesome");.
=== Program that searches Google (C#) ===
using System.Diagnostics;
class Program
{
static void Main()
{
// Search Google for this.
SearchGoogle("VB.NET awesome");
}
/// <summary>
/// Search Google for this term.
/// </summary>
static void SearchGoogle(string t)
{
Process.Start("http://google.com/search?q=" + t);
}
}Web browser note. You shouldn't specify a certain web browser, such as IEXPLORE. It has no advantages and will make Chrome, Firefox, or Safari users unhappy. Windows selects the default browser on its own.
You can open Microsoft Word or other applications from Office by specifying the EXE name. Here we force Windows to open files in Microsoft Word. WINWORD.EXE must open the file. Call the following method with the path of a file, such as OpenMicrosoftWord(@"C:\Folder\file.txt");.
=== Program that starts WINWORD.EXE (C#) ===
using System.Diagnostics;
class Program
{
static void Main()
{
// A.
// Open specified Word file.
OpenMicrosoftWord(@"C:\Users\Sam\Documents\Gears.docx");
}
/// <summary>
/// Open specified word document.
/// </summary>
static void OpenMicrosoftWord(string f)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "WINWORD.EXE";
startInfo.Arguments = f;
Process.Start(startInfo);
}
}Notes on example. Above we use ProcessStartInfo. In it we can store properties of the process. Here is the result on my system. You could also specify EXCEL.EXE for Microsoft Excel or POWERPNT.EXE for PowerPoint.
Here we see an incomplete listing of Process properties in C#. You will use most of these when starting programs from your program, and knowing about all of them is valuable.
Class: ProcessStartInfo
Notes: Stores information about the process.
Property: FileName
Notes: The program or filename you want to run.
It can be a file such as "example.txt".
It can be a program such as "WINWORD.EXE".
Property: Arguments
Notes: Stores the arguments, such as -flags or filename.
Property: CreateNoWindow
Notes: Allows you to run a command line program silently.
It does not flash a console window.
Property: WindowStyle
Notes: Use this to set windows as hidden.
The author has used ProcessWindowStyle.Hidden often.
Property: UserName
WorkingDirectory
Domain
Notes: These control OS-specific parameters.
For more complex situations,
where Windows features are used extensively.You can run any executable, but may need to use more properties of ProcessStartInfo. Here we run an old program called dcm2jpg.exe. This program converts DICOM images back and forth. The example is a bit more complicated.
=== Program that runs EXE (C#) ===
using System.Diagnostics;
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.
}
}
}What it does. It first creates a ProcessStartInfo, and we use the CreateNoWindow and UseShellExecute options to control some command-line program options. The program we are running is called dcm2jpg.exe, and the Arguments string is set.
Here we note that you can combine the Process.Start method as shown in this article with a console program that receives a parameter list from a string[] args method on invocation. You must specify and test the string[] array parameter in the Main method. You can find more details on command-line arguments on this site. [C# Main Args Examples - dotnetperls.com]
Here we point to some other resources on this web site regarding processes on Windows. I have used Windows EXE bundling more than once. Read my article about how to put 7-Zip in a Windows Forms program. I have a 7-Zip tutorial for Windows here. [.NET 7-Zip Executable Tutorial - dotnetperls.com]
Can I use processes with ThreadPool? Yes, and this works really well. See my work with ThreadPool and progress bars. I got an EXE to run concurrently over more than 1 processor for a speed boost. [C# ThreadPool Usage - dotneteperls.com]
Here we saw how you can use the Process.Start method in System.Diagnostics. Use the approaches here to run commands on Windows in .NET. This article may be useful for people working on problems similar to mine. We saw syntax hints and useful examples with the command line.