Launch any executable (Microsoft Word, Notepad, or a web browser) using Process.Start. The user must then be able to view documents, both on and off the web, or run another program or command line tool. You need to run commands in programs to perform tasks in various applications.
We must use System.Diagnostics. Our sample project is a Windows Forms application. These steps and approaches will work in many different configurations, however. Get Visual Studio set up on a source file you want to add Process.Start to.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
class StartPrograms
{
// Methods on this page can be put here. You don't need to name it
// anything in specific. Just remember System.Diagnostic above.
}
By specifying the file name alone. We have a text file you know the location of. (You can get the location of the file through an OpenFileDialog.) With the following code on my system, Microsoft Word 2007 opens the file example.txt, because on my system Microsoft Word is the default TXT editor. After this example, we will use Microsoft Word again.
// Open the file "example.txt" that is in the same directory as
// your .exe file you are running.
Process.Start("example.txt");
By specifying the EXE name. Here we force Windows to open files in Microsoft Word. We must specify that Word.exe must open the file. Call the following method with the path of a file, such as OpenLocalWord(@"C:\Folder\file.txt");.
/// <summary>
/// Call this function to open a file at the path (which includes the file name)
/// and open it in WINWORD.EXE, Microsoft Word of any version.
/// </summary>
public void OpenLocalWord(string filePath)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "WINWORD.EXE";
startInfo.Arguments = filePath;
Process.Start(startInfo);
}
Notes. Above we use ProcessStartInfo. That is just a useful class we can store various properties of our required process. Let's look at a few properties that are useful here. There are lots more, but this isn't an exhaustive reference.
| Property or object | Use |
| ProcessStartInfo | Make as a new object to store information about the process you want to run |
| FileName |
The program or filename you want to run; it can be a file such as "example.txt"
or a program such as "WINWORD.EXE" (read more about file handling) |
| Arguments | String property on ProcessStartInfo that stores the arguments, which can include -flags or a filename |
Specify the URL and send it to Windows. Here we launch a Google search for a word, such as "India". It is always good to give people as much information as possible, and Google has a lot more information than I do. Now, let's say your users run Firefox, Internet Explorer, Opera, or Safari on Windows. Call the following code with OpenSearchTerm("India");.
/// <summary>
/// This method receives the search term you want to show results for.
/// Then, it starts the user's default browser and specifies a URL
/// for a Google search.
/// </summary>
public void OpenSearchTerm(string searchTerm)
{
Process.Start("http://google.com/search?q=" + searchTerm);
}
Don't specify what browser. You shouldn't specify a certain web browser, such as IEXPLORE. Doing so has no advantages and will make Opera, Firefox, or Safari users unhappy. Windows always selects the default browser on its own, so let it do its thing. (You also should escape entities in the above function.)
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, but much of the code is not really necessary in many cases.
private void LaunchCommandLineApp()
{
// Launch a command-line application, with some options set.
// Arguments here are just application-specific and can be
// used to control the output format of the application being started.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "dcm2jpg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-f j -o \"" + _cacheDir + "\" -z 1.0 -s y " + target;
try
{
// Start the process with the info we specified.
// Call WaitForExit and the Close. An exception is thrown
// if something goes wrong.
Process exeProcess = Process.Start(startInfo);
exeProcess.WaitForExit();
exeProcess.Close();
}
catch
{
// Log error.
return;
}
}
Important parts. Here are some relevant pieces of the above program. 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 just something we built up from other parts of the UI.
Use the approaches here to run commands on Windows in .NET. This article isn't a tell-all about Process.Start or similar methods, but it may be useful for people working on problems similar to mine. We see syntax hints and useful examples and ideas for the command lines.