C# Tips: Running External Applications from Windows Forms

Abstract

Michael Agranov shares some C# Tips for development tasks.

These code samples demonstrate the following development tasks:

  • How to open an external application, such as Microsoft Word.
  • How to open a command prompt in the specified folder, run the command, and redirect the command output to the hosting application.
  • How to open a web browser with a search query.

From the Download section of this tutorial, download the project called ExternalApps. Build and run this application. The following screen shot shows how your application will appear with the Microsoft Word file specified, and after running the command line process.

External Apps Application

Figure 1: External Apps Application

Open Microsoft Word with a Specified File

Your test project contains the TestDocument.docx file in the “..\ExternalApps\bin\Debug” folder. Click on the Specify File button and select TestDocument.docx. After the file name with its complete file path appears in the text box, click the Open in Word button. TestDocument.docx is opened in Microsoft Word.

The following code shows how to retrieve the file name with its complete path and store its value in the text box textBox1. You then use the ProcessStartInfo class property members to specify that you intend to open your test file in Microsoft Word.

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.InitialDirectory = Application.StartupPath;
    openFileDialog.Filter = "WORD Files(*.doc;*.docx)|*.doc;*.docx|All Files|*.*";
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        string path = openFileDialog.FileName.ToString();
        textBox1.Text = path;
    }
}
 
private void button2_Click(object sender, EventArgs e)
{
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = "WINWORD.EXE";
    startInfo.Arguments = textBox1.Text;
    Process.Start(startInfo);
}

Open Command Prompt in the Specified Folder, Run the Command, and Redirect Output to the Windows Forms Application

Click on the Run Command button. You can see the result of running this application in Figure 1.

The following code shows how to create a command line process that starts in the “c:\” folder, executes the dir command, and redirects the dir command output to the Windows Forms RichTextBox control.

private void button3_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process process = new System.Diagnostics.Process();
    process.StartInfo.WorkingDirectory = "c:\\";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.FileName = "cmd.exe";
    process.StartInfo.Arguments = "/c dir";
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.Start();
    string output = process.StandardOutput.ReadToEnd();
    process.Close();
    richTextBox1.Text = output;
}

Open a Web Browser with a URL and a Search Query

Click on the Run Browsers button. As a result of running this application the following happens:

  • The Internet Explorer web browser starts with the www.microsoft.com URL.
  • The Google Chrome web browser starts with the Bing query (“C# examples”).

The following simple code shows how to open browsers.

private void button4_Click(object sender, EventArgs e)
{
    Process.Start("Chrome.exe", "http://www.bing.com/search?q=C%23+examples");
    Process.Start("IExplore.exe", "www.microsoft.com");
}

Further Reading and Reference

OpenFileDialog Class

ProcessStartInfo Class

Process Class

Process.Start Method



Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 18, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT While the idea of using facial and or gesture recognitions to create a modern, intuitive game seems attractive, some developers may want to leverage Unity 3D as a way to accelerate their development. There are many different ways in which Intel and Unity Technologies have been working together to helps speed the develop of games with the Intel® RealSense™ SDK (Software Developer Kit), so come hear from a panel of experts on what we've done …

  • Several technologies are driving big changes at federal agencies, but at the forefront is cloud computing. Cloud -- the use of remote, Internet-hosted servers for storing, managing and processing data in place of on-site technology -- is helping along technologies such as those related to collaboration and enabling new ones, such as the analysis of Big Data. Cloud is complicated but also growing in importance at federal agencies. Read this technology article to learn how governmental entities are creating a …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date