Mar 25, 2012

How to Shut down or Run any process in computer using C# ?


Now we are going to learn, how to run any process and its a shut down using C#.Net. To achieve this we have to use System.Diagnostics Namespace. 

The System.Diagnostics namespaces contain types that enable you to interact with system processes, event logs, and performance counters. Child namespaces contain types to interact with code analysis tools, to support contracts, to extend design-time support for application monitoring and instrumentation, to log event data using the Event Tracing for Windows (ETW) tracing subsystem, to read to and write from event logs and collect performance data, and to read and write debug symbol information.

The following example first spawns an instance of Internet Explorer and displays the contents of the “Favorites” folder in the browser. It then starts some other instances of Internet Explorer and displays some specific pages or sites. Finally it starts Internet Explorer with the window being minimized while navigating to a specific site.

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace TestProcess
    class MyProcess
        // Opens the Internet Explorer application.
       private void OpenApplication(string myFolderPath)
            // Start Internet Explorer. Defaults to the home page.

            // Display the contents of the specified folder in the browser.

        // Opens urls and .html documents using Internet Explorer.
        private void OpenWithArguments()
            // url's are not considered as documents. They can only be opened by passing them as   arguments.
            Process.Start("IExplore.exe", "");

            // Start a Web page using a browser associated with .html and .asp files.
            Process.Start("IExplore.exe", @"C:\FolderPath\myFile.html");
            Process.Start("IExplore.exe", @"C:\FolderPath\myFile.asp");

        // Uses the ProcessStartInfo class to start new processes,
        // both in a minimized mode.
        private void OpenWithStartInfo()
            ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
            startInfo.WindowStyle = ProcessWindowStyle.Minimized;

            startInfo.Arguments = "";

        static void Main()
            // Get the path that stores favorite links.
            string myFavoritesPath =

            MyProcess myProcess = new MyProcess();

Shut down the System:
The easiest way to shutdown, restart, or hibernate a computer programmatically using C# is to use the shutdown command-line tool that comes with Windows. This tool has a lot of options, for example
 /l logs of the user,
/s shuts down the computer,
/r restarts the computer,
/h hibernates the computer.
Note: To get the list of available options, type help shutdown in the Command Prompt.

To execute shutdown from C# use the follow statement: 
// First parameter is the command, and the second is its argument
System. Diagnostics.Process.Start("shutdown","/s");