. NET run the program as an administrator

Source: Internet
Author: User
Tags mssqlserver

usingSystem;usingSystem.ComponentModel;usingSystem.Diagnostics;usingSystem.Windows.Forms;namespacerunasadministratordemo{/// <summary>    ///     . NET run the program as an administrator///LDH @ 2018-1-5/// </summary>    Internal classProgram {/// <summary>        ///Database service Operations/// </summary>         Public enumdatabaseserviceoperation {/// <summary>            ///start the Ms SQL Server service/// </summary>[Description ("start the Ms SQL Server service")] Startmssqlserver =1,            /// <summary>            ///terminating the Ms SQL Server service/// </summary>[Description ("terminating the Ms SQL Server service")] Stopmssqlserver =2,            /// <summary>            ///start My Sql service/// </summary>[Description ("start My MySQL service")] Startmysql =3,            /// <summary>            ///Terminate MySQL service/// </summary>[Description ("Terminate MySQL service")] Stopmysql =4,            /// <summary>            ///Start Redis Service/// </summary>[Description ("Start Redis Service")] Startredis =5,            /// <summary>            ///Terminate Redis service/// </summary>[Description ("Terminate Redis service")] Stopredis =6,            /// <summary>            ///Start MongoDB Service/// </summary>[Description ("Start MongoDB Service")] Startmongodb =7,            /// <summary>            ///End MongoDB Service/// </summary>[Description ("End MongoDB Service")] Stopmongodb =8        }        Private Static voidMain () {Console.title=". NET run the appropriate program as Administrator by LDH @"+ DateTime.Now.ToString ("YYYY-MM-DD HH:mm:ss");            Howtorunasadministrator ();        Console.readkey (); }        /// <summary>        ///how to run as Administrator/// </summary>        Private Static voidHowtorunasadministrator () { while(true) {Console.backgroundcolor=Consolecolor.blue; Console.foregroundcolor=Consolecolor.white;                Console.clear (); Console.WriteLine ("\ t Enter the specified number and perform the appropriate action! ");                PrintLine (); Console.WriteLine (""0" Exit program");                PrintLine (); Console.WriteLine (""1" starts Ms Sql Sever"); Console.WriteLine (""2" terminates Ms SQL Server");                PrintLine (); Console.WriteLine (""3" Start the MySQL service"); Console.WriteLine (""4" Termination of MySQL service");                PrintLine (); Console.WriteLine (""5" Start Redis service"); Console.WriteLine (""6" Termination of Redis service");                PrintLine (); Console.WriteLine (""7" Start MongoDB service"); Console.WriteLine (""8" Stop MongoDB service");                PrintLine (); varinput =Console.ReadLine (); varOperatenum =Convert.ToInt32 (input); if(Operatenum! =0)                {                    varProcess =NewProcess {startinfo={Verb="runas", UseShellExecute=true, FileName="Cmd.exe", Arguments=getargumentstring ((databaseserviceoperation) Operatenum)}}; Process.                   Start (); Process.                           WaitForExit (); MessageBox.Show ("Successful Execution! ","Warm Tips", MessageBoxButtons.OK, messageboxicon.information); }                Else{environment.exit (0); }            }        }        /// <summary>        ///gets the corresponding execution command string based on the type of database service/// </summary>        /// <param name= "Operation" >Types of database services</param>        /// <returns>the corresponding service execution command string</returns>        Private Static stringgetargumentstring (Databaseserviceoperation operation) {varArgumentsstr =string.            Empty; Switch(operation) { CaseDatabaseServiceOperation.StartMsSqlServer:argumentsStr="/C net start MSSQLServer";  Break;  CaseDatabaseServiceOperation.StopMsSqlServer:argumentsStr="/C net stop mssqlserver";  Break;  CaseDatabaseServiceOperation.StartMySql:argumentsStr="/C net start MySQL";  Break;  CaseDatabaseServiceOperation.StopMySql:argumentsStr="/C-net stop MySQL";  Break;  CaseDatabaseServiceOperation.StartRedis:argumentsStr="/C net start Redis";  Break;  CaseDatabaseServiceOperation.StopRedis:argumentsStr="/C-net stop Redis";  Break;  CaseDatabaseServiceOperation.StartMongoDb:argumentsStr="/C net start MongoDB";  Break;  CaseDatabaseServiceOperation.StopMongoDb:argumentsStr="/C net stop MongoDB";  Break; }            returnArgumentsstr; }        /// <summary>        ///Split Line/// </summary>        Private Static voidPrintLine () {
Console.WriteLine ("-----------------------------------------------------"); } }}

Operating effect:

. NET run the program as an administrator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.