Mar 25, 2012

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

2 comments

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.

Example:
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.
            Process.Start("IExplore.exe");

            // Display the contents of the specified folder in the browser.
            Process.Start(myFolderPath);
        }

        // 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", " http://laddukavala.blogspot.in");

            // 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;

            Process.Start(startInfo);
            startInfo.Arguments = " http://laddukavala.blogspot.in";
            Process.Start(startInfo);
        }

        static void Main()
        {
            // Get the path that stores favorite links.
            string myFavoritesPath =
                Environment.GetFolderPath(Environment.SpecialFolder.Favorites);

            MyProcess myProcess = new MyProcess();
           myProcess.OpenApplication(myFavoritesPath);
            myProcess.OpenWithArguments();
            myProcess.OpenWithStartInfo();
        }
    }
}

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");

2 comments :