C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Now: Please get Visual Studio set up on the source file. You will need to update some of the paths to make these programs work.
Next: This example sends the C:\ directory name to the operating system. Windows Explorer will open the folder in a new window.
Arguments: The Process.Start method has overloaded forms. So you can call it with more than argument.
C# program that opens directory
using System.Diagnostics;
class Program
{
static void Main()
{
// Use Process.Start here.
Process.Start("C:\\");
}
}
Tip: On many systems, Notepad will instead open the file. The file name is passed to Windows to handle with the default program.
C# program that opens text file
using System.Diagnostics;
class Program
{
static void Main()
{
// Open the file "example.txt".
// ... It must be in the same directory as the .exe file.
Process.Start("example.txt");
}
}
Here: We launch a DuckDuckGo search with a specific query. You can change the address to another location.
Usually: We should not specify a browser. This may make users unhappy. Windows selects the default browser on its own.
C# program that launches web browser
using System.Diagnostics;
class Program
{
static void Main()
{
// Call method.
SearchDuckDuckGo("cat pictures");
}
static void SearchDuckDuckGo(string term)
{
// Search DuckDuckGo for this term.
Process.Start("https://www.duckduckgo.com/?q=" + term);
}
}
OpenMicrosoftWord: WINWORD.EXE must open the file. Please call OpenMicrosoftWord() with the path of a file.
ProcessStartInfo: In this example we use ProcessStartInfo. In it, we can store a process' properties: details of its execution.
Tip: We could also specify EXCEL.EXE for Microsoft Excel. Or we could use POWERPNT.EXE for PowerPoint.
C# program that starts WINWORD.EXE
using System.Diagnostics;
class Program
{
static void Main()
{
// ... Open specified Word file.
OpenMicrosoftWord(@"C:\Users\Sam\Documents\Gears.docx");
}
/// <summary>
/// Open specified word document.
/// </summary>
static void OpenMicrosoftWord(string file)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "WINWORD.EXE";
startInfo.Arguments = file;
Process.Start(startInfo);
}
}
Part 1: This example first creates a ProcessStartInfo. We use CreateNoWindow and UseShellExecute to control some command-line options.
Part 2: We set some arguments to indicate to the executable what directories were are using.
Part 3: We invoke Process.Start, and then call WaitForExit to wait for the executable to finish its task.
C# program that runs EXE
using System.Diagnostics;
class Program
{
static void Main()
{
LaunchCommandLineApp();
}
static void LaunchCommandLineApp()
{
const string ex1 = "C:\\";
const string ex2 = "C:\\Dir";
// Part 1: use ProcessStartInfo class.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "dcm2jpg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
// Part 2: set arguments.
startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;
try
{
// Part 3: start with the info we specified.
// ... Call WaitForExit.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
// Log error.
}
}
}
Note: Process.GetProcesses looks at all processes currently running. With this method, we enumerate the active processes.
Info: GetProcesses receives no arguments or one argument of the target machine name (not shown). It returns a Process array.
Foreach: We use a foreach-loop to access all the Processes returned by the method. We access each Process's Id.
ForeachC# program that uses GetProcesses method
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
// Show all processes on the local computer.
Process[] processes = Process.GetProcesses();
// Display count.
Console.WriteLine("Count: {0}", processes.Length);
// Loop over processes.
foreach (Process process in processes)
{
Console.WriteLine(process.Id);
}
}
}
Output
Count: 70
388
5312
1564
972
2152
936
3132....
Here: We use Process.GetProcessesByName in a while-true loop. We use the argument "chrome" to GetProcessesByName.
WhileName: This returns an array of all processes with name "chrome." Then, we sleep for 5 seconds before calling the method again.
SleepResult: Because of its multi-process model, Chrome can run in several processes at once.
Tip: Avoid the "exe" part. The method did not work when I tried using the string "chrome.exe" as the argument.
C# program that uses GetProcessesByName
using System;
using System.Diagnostics;
using System.Threading;
class Program
{
static void Main()
{
while (true)
{
// Omit the exe part.
Process[] chromes = Process.GetProcessesByName("chrome");
Console.WriteLine("{0} chrome processes", chromes.Length);
Thread.Sleep(5000);
}
}
}
Output
0 chrome processes
3 chrome processes
4 chrome processes
5 chrome processes
5 chrome processes
5 chrome processes
Info: RedirectStandardOutput eliminates the need for output files. It allows us to use a console program directly inside a C# program.
StreamReader: We redirect into a StreamReader. With ReadToEnd() we can read the entire output of an EXE into a string.
StreamReaderTip: To redirect the output of a process, set UseShellExecute to false and RedirectStandardOutput to true.
Also: You must specify the file name (with the FileName property) before calling Process.Start.
C# program that redirects standard output
using System;
using System.Diagnostics;
using System.IO;
class Program
{
static void Main()
{
//
// Set up the process with the ProcessStartInfo class.
//
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @"C:\7za.exe"; // Specify exe name.
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
//
// Start the process.
//
using (Process process = Process.Start(start))
{
//
// Read in all the text from the process with the StreamReader.
//
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
}
}
Output
7-Zip (A) 4.60 beta Copyright (c) 1999-2008 Igor Pavlov 2008-08-19
Usage: 7za <command> [<switches>...] <archive_name> [<file_names>...]
[<@listfiles...>]
Example: We start the Notepad program, and then wait one second. Finally, we call the Kill method on the process variable.
Tip: It is probably more common to use the Kill method to search for and stop applications that are not working.
So: First loop through the running processes. Then, you can acquire a reference to the process and call Kill.
C# program that uses Process Kill method
using System.Diagnostics;
using System.Threading;
class Program
{
static void Main()
{
// Start notepad.
Process process = Process.Start("notepad.exe");
// Wait one second.
Thread.Sleep(1000);
// End notepad.
process.Kill();
}
}