WorldofASP.NET : ASP.NET Directory, Tutorial, Hosting, and Source Code
You are 1 of 38 users


WorldofASP.NET >> Csharp Programming >> Unedited Csharp

Starting Windows Command Process in C#

Example on how to start windows command syntax by backup MySQL Database using MySQLDump
Published Date : 09 Nov 2008
Author : Hans Candra
Language : C#
Platform : .NET
Technology : ASP.NET
Views : 2267
Rating : (0 votes so far)



Introduction

A C# program can launch another program using the Process class. The Process class is part of the System.Diagnostics namespace. You start another program by instantiating a Process object, setting members of it's StartInfo property, and invoking it's Start() method.

Main

We can use System.Diagnostics. Our exsample below will show you how to start a windows command prompt using C# Syntax.

       private string ExecuteProcess(string ProcessFileName, string ProcessArguments)
        {
            string Result = "";
            Process oProcess = null;
            try
            {
                ProcessStartInfo oInfo = 
new ProcessStartInfo(ProcessFileName, ProcessArguments);
                oInfo.CreateNoWindow = true;
                oInfo.UseShellExecute = false;
                oInfo.RedirectStandardError = true;
                oInfo.RedirectStandardOutput = true;
                oInfo.RedirectStandardInput = true;
                oProcess = Process.Start(oInfo);

                oProcess.WaitForExit(20000);
                if (!oProcess.HasExited)
                {
                    oProcess.Kill();
                }
                Result = oProcess.StandardError.ReadToEnd();
            }
            catch (Exception ex)
            {
                
                Result = ex.ToString();
            }
            finally
            {
                oProcess.Close();
            }
            return Result;
        }

When the syntax above runs, it will execute the Windows process specify in processFileName. The first thing the program does is instantiate a Process object, an identifier that happens to have the same name as the program that will be executed. We specify what program will be executed. This happens by assigning the program name to the FileName member of the Process object's StartInfo property. Here's the line from the code:

               ProcessStartInfo oInfo = 
new ProcessStartInfo(ProcessFileName, ProcessArguments);

The StartInfo property also has a member called Arguments, which is used to specify command-line options that are passed to the program. In the above example, the string ProcessArguments is passed as an argument, so the application will know what file to open when it is executed:

The program is then executed by invoking the Start() method. Then next syntax is to specify the process windows will be shown or not. RedirectStandardError, RedirectStandardOutput, and RedirectStandardInput is to show output information of the process whether it is success executed or failed with error message.

Abouve syntax will be run ok, but sometimes depends of your process, we need to change some of the parameter in it. If you want to do some research, then can change it for example createnewwindow or useshellexecute.

Next we will look at the method to execute the windows process itself.

        public string ExecuteCommand()
        {
            string s = ExecuteProcess("cmd.exe", @"notepad.exe");
            return s;

        }

The results of this program are the same as if the instructions below were typed on the command-line:

C:\notepad.exe

Conclusion

This article showed how to start a new process from C#. This is usefull when you want to launch another program from your application. The example shown, demonstrated how to open the run command prompt to execute another process.

References

Include all the useful links or references that can help users learn about this tutorial

  1. Managing Windows Process
  2. Monitoring and Managing Windows Process




    Other Related and Popular Articles :

    Upload and retrieve image to and from database
    using FileUpload control to store image in SQL Server

    Handling Cookie in ASP.NET
    Create a class to insert and retrieve cookie

    Launch Internet Explorer from Windows Application
    Exposing SHDocVw class to open IE

    Value Type and Reference Type in C
    It will give a clear idea about value type and reference type in c#. Thus help to readers to use proper data type in coding in their application.

    Convert and Bind Data to a Vertical DataGrid
    How to convert a DataBase Table to a Vertical View and Bind it to a DataGrid

    CaptchaImage
    CaptchaImage


    Author Profile : Hans Candra

    Click here to view Author Profile


    How would you rate the quality of this content?
    Poor Excellent

    Comments

    Leave New Comments


    Article Content copyright by Hans Candra
    Everything else Copyright © by WorldofASP.NET 2009

    Category
    .NET 3.5
    AJAX and ATLAS
    ASP.NET
    C# Programming
    Classic ASP
    Enterprise Systems
    General .NET
    VB.NET Programming
    Announcements
    Earn Cash by writing an article or review
    For more info Click here







    Legend : - Within 3 Days - Within 6 Days - Within 9 Days

    Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 | Link To Us | Contact Us
    © 2002-2009 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
    Our Partners : ASP.NET Web Hosting | Windows Web Hosting | ASP.NET Hosting | Phone Card | PHP Directory | Bangkok Hotels |Calling Card |Stock Investing