Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute/open a program in c#

Tags:

c#

.net

Is there a solution/references on how to open or execute certain window programs in C#? For example if i want to open WinZIP or notepad application?

Example on the line of codes are more helpful. But anything are welcomed.

thank you.

like image 462
user147685 Avatar asked Aug 27 '09 06:08

user147685


People also ask

How do I run a program?

In Windows to execute a program, double-click the executable file or double-click the shortcut icon pointing to the executable file. If you have a hard time double-clicking an icon, click the icon once to highlight it and then press Enter on the keyboard.

How do I open C in terminal?

To compile a simple C program, we use the Linux command-line tool, the terminal. To open the terminal, you can use the Ubuntu Dash or the key combination Ctrl+Alt+T.


1 Answers

You can use the System.Diagnostics.Process.Start method.

Process.Start("notepad.exe");

It will work with files that have associated a default program:

Process.Start(@"C:\path\to\file.zip"); 

Will open the file with its default application.

And even with URLs to open the browser:

Process.Start("http://stackoverflow.com"); // open with default browser

Agree with @Oliver, ProcessStartInfo gives you a lot of more control over the process, an example:

ProcessStartInfo startInfo = new ProcessStartInfo();

startInfo.FileName = "notepad.exe";
startInfo.Arguments = "file.txt";
startInfo.WorkingDirectory = @"C:\path\to";
startInfo.WindowStyle = ProcessWindowStyle.Maximized;

Process process = Process.Start(startInfo);

// Wait 10 seconds for process to finish...
if (process.WaitForExit(10000))
{
     // Process terminated in less than 10 seconds.
}
else
{
     // Timed out
}
like image 88
Christian C. Salvadó Avatar answered Oct 03 '22 18:10

Christian C. Salvadó