dcsimg

The Process Class and .NET

WEBINAR:
On-Demand

Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame


Introduction

I have mentioned and demonstrated the Process class before in some of my earlier articles, but I haven't gone in too much detail as to what you can do with it. This article aims to demonstrate the most common uses of the Process class so that you can launch separate processes (programs) in a variety of ways.

What Is a Process?

The Process component gives access to a process that may running on a computer. A process in this case is simply a running application, whether it be in the background or in the foreground. The Process component can start, stop, control, and monitor applications.

You have the ability, with the Process component, to do all the abovementioned tasks with windowless programs, services, and any other programs. You also can start applications with parameters, or launch default applications based on the file extension of the file supplied to the Process component.

Let's create a program!

Open Visual Studio and create a new Windows Forms application in either Visual basic or in C#. Once the design window has loaded fully, add four buttons to the Form, as shown in Figure 1.

Design
Figure 1: Design

Each button represents a different way in which will make use of the Process component to launch an application.

Now, on to the code.

Add the following procedures to your code:

C#

   void OpenWithoutWindow()
   {

         using (Process proc = new Process())
         {
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.FileName = "C:\\Notepad.exe";
            proc.StartInfo.CreateNoWindow = true;
            proc.Start();
         }

   }
   void OpenNormally(string strFav)
   {
      Process.Start("chrome.exe");

      Process.Start(strFav);
   }

   void OpenWithArgs()
   {

      Process.Start("Chrome.exe", "www.codeguru.com");

      Process.Start("Chrome.exe", "C:\\Test\\Test.htm");
   }

   void OpenWithStartInfo()
   {
      ProcessStartInfo piStart = new
         ProcessStartInfo("Chrome.exe");
      piStart.WindowStyle = ProcessWindowStyle.Minimized;

      Process.Start(piStart);

      piStart.Arguments = "www.codeguru.com";

      Process.Start(piStart);
   }

VB.NET

   Private Sub OpenWithoutWindow()

      Using proc As Process = New Process()

         proc.StartInfo.UseShellExecute = False

         proc.StartInfo.FileName = "C:\Notepad.exe"
         proc.StartInfo.CreateNoWindow = True
         proc.Start()

      End Using

   End Sub

   Private Sub OpenNormally(ByVal strFav As String)

      Process.Start("chrome.exe")

      Process.Start(strFav)

   End Sub

   Private Sub OpenWithArgs()

      Process.Start("Chrome.exe", "www.codeguru.com")
      Process.Start("Chrome.exe", "C:\Test\Test.htm")

   End Sub

   Private Sub OpenWithStartInfo()

      Dim piStart As ProcessStartInfo = _
         New ProcessStartInfo("Chrome.exe")
      piStart.WindowStyle = ProcessWindowStyle.Minimized
      Process.Start(piStart)
      piStart.Arguments = "www.codeguru.com"
      Process.Start(piStart)

   End Sub

The OpenWithoutWindow procedure creates a new Process object and sets its UseShellExecute property to false. This prevents the default application from opening the file. Which file or process? Notepad.exe, in this case, as supplied on the next line. Then, the CreateNoWindow property is set to true, which means that the process is running, but you cannot see it because it doesn't have a window for the user to control. Lastly, the process is started.

The OpenNormally procedure accepts a parameter named strFav. The Chrome browser gets started and then the URL supplied through the strFav argument is opened inside of the Chrome web browser. if you do not have Chrome, make use of any other browser you may have, such as IExplore for Internet Explorer.

The OpenWithArgs procedure supplies a second argument to the Start call of the Process object. The first argument is the physical application to run; the second argument is the file (or in this case) the URL it should open.

The OpenWithStartInfo procedure uses the ProcessStartInfo object to specify what application to start, how the application should be started (Minimized, Normally, Maximized), and optional arguments for the Process object which will ultimately start the ProcessStartInfo object.

Add the calls to the above procedures.

C#

   private void Button1_Click(object sender, EventArgs e)
   {
      OpenWithoutWindow();
   }

   private void Button2_Click(object sender, EventArgs e)
   {

      string strFav = Environment.GetFolderPath(Environment
         .SpecialFolder.Favorites);

      OpenNormally(strFav);
   }

   private void Button3_Click(object sender, EventArgs e)
   {
      OpenWithArgs();
   }

   private void Button4_Click(object sender, EventArgs e)
   {
      OpenWithStartInfo();
   }

VB.NET

   Private Sub Button1_Click(ByVal sender As Object, _
         ByVal e As EventArgs)
      OpenWithoutWindow()
   End Sub

   Private Sub Button2_Click(ByVal sender As Object, _
         ByVal e As EventArgs)
      Dim strFav As String = Environment.GetFolderPath(Environment _
         .SpecialFolder.Favorites)
      OpenNormally(strFav)
   End Sub

   Private Sub Button3_Click(ByVal sender As Object, _
         ByVal e As EventArgs)
      OpenWithArgs()
   End Sub

   Private Sub Button4_Click(ByVal sender As Object, _
         ByVal e As EventArgs)
      OpenWithStartInfo()
   End Sub

All buttons simply call the procedure name to run it. For the OpenNormally procedure, the physical path to the user's Favourites is supplied.

Conclusion

The Process object is quite versatile and enables you to open any document in its default application, or any program, with or without arguments.



This article was originally published on March 31st, 2020

About the Author

Hannes DuPreez

Hannes du Preez is a self-taught developer who started learning to program in the days of QBasic. He has written several articles over the years detailing his programming quests and adventures. .NET is his second love, just after his wife and kid. He has always been an avid supporter of .NET since the beginning and is an expert in VB and C#. He was given the Microsoft Most Valuable Professional Award for .NET (2008–2017). He has worked as a moderator and an article reviewer on online forums and currently works as a C# developer and writes articles for CodeGuru.com, Developer.com, DevX.com, and the Database journal.
His first book Visual Studio 2019 In Depth is currently on sale on Amazon and Bpb Publications.

You could reach him at: ojdupreez1978[at]gmail[dot]com

Related Articles

Most Popular Programming Stories

More for Developers

RSS Feeds

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