码迷,mamicode.com
首页 > Web开发 > 详细

.NET以管理员身份运行程序

时间:2018-01-05 15:16:02      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:数据   my sql   技术   rate   mon   operation   private   script   esc   

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;

namespace RunAsAdministratorDemo
{
    /// <summary>
    ///     .NET以管理员身份运行程序
    ///     LDH @ 2018-1-5
    /// </summary>
    internal class Program
    {
        /// <summary>
        ///     数据库服务操作
        /// </summary>
        public enum DatabaseServiceOperation
        {
            /// <summary>
            ///     启动 Ms Sql Server 服务
            /// </summary>
            [Description("启动 Ms Sql Server 服务")] StartMsSqlServer = 1,

            /// <summary>
            ///     终止 Ms Sql Server 服务
            /// </summary>
            [Description("终止 Ms Sql Server 服务")] StopMsSqlServer = 2,

            /// <summary>
            ///     启动 My Sql  服务
            /// </summary>
            [Description("启动 My MySQL  服务")] StartMySql = 3,

            /// <summary>
            ///     终止 MySQL 服务
            /// </summary>
            [Description("终止 MySQL 服务")] StopMySql = 4,

            /// <summary>
            ///     启动 Redis  服务
            /// </summary>
            [Description("启动 Redis  服务")] StartRedis = 5,

            /// <summary>
            ///     终止 Redis 服务
            /// </summary>
            [Description("终止 Redis 服务")] StopRedis = 6,

            /// <summary>
            ///     启动 MongoDB  服务
            /// </summary>
            [Description("启动 MongoDB  服务")] StartMongoDb = 7,

            /// <summary>
            ///     终止 MongoDB 服务
            /// </summary>
            [Description("终止 MongoDB 服务")] StopMongoDb = 8
        }

        private static void Main()
        {
            Console.Title = ".NET以管理员身份运行相应的程序 By LDH @ " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            HowToRunAsAdministrator();

            Console.ReadKey();
        }

        /// <summary>
        ///     如何以管理员身份运行
        /// </summary>
        private static void HowToRunAsAdministrator()
        {
            while (true)
            {
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.ForegroundColor = ConsoleColor.White;
                Console.Clear();

                Console.WriteLine("\t 输入指定的数字,执行相应操作!");

                PrintLine();

                Console.WriteLine("【0】退出程序");

                PrintLine();

                Console.WriteLine("【1】启动 Ms Sql Sever");
                Console.WriteLine("【2】终止 Ms Sql Server");

                PrintLine();

                Console.WriteLine("【3】启动 MySQL 服务");
                Console.WriteLine("【4】终止 MySQL  服务");

                PrintLine();

                Console.WriteLine("【5】启动 Redis  服务");
                Console.WriteLine("【6】终止 Redis  服务");

                PrintLine();

                Console.WriteLine("【7】启动 MongoDB  服务");
                Console.WriteLine("【8】终止 MongoDB  服务");

                PrintLine();

                var input = Console.ReadLine();

                var operateNum = Convert.ToInt32(input);
                if (operateNum != 0)
                {
                    var process = new Process
                    {
                        StartInfo =
                        {
                            Verb = "runas",
                            UseShellExecute = true,
                            FileName = "cmd.exe",
                            Arguments = GetArgumentString((DatabaseServiceOperation) operateNum)
                        }
                    };

                   process.Start();
                   process.WaitForExit();
        
                   MessageBox.Show("执行成功!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    Environment.Exit(0);
                }
            }
        }

        /// <summary>
        ///     根据数据库服务的类型获取对应的执行命令字符串
        /// </summary>
        /// <param name="operation">数据库服务的类型</param>
        /// <returns>对应的服务执行命令字符串</returns>
        private static string GetArgumentString(DatabaseServiceOperation operation)
        {
            var argumentsStr = string.Empty;

            switch (operation)
            {
                case DatabaseServiceOperation.StartMsSqlServer:
                    argumentsStr = "/C net start mssqlserver";
                    break;

                case DatabaseServiceOperation.StopMsSqlServer:
                    argumentsStr = "/C net stop mssqlserver";
                    break;

                case DatabaseServiceOperation.StartMySql:
                    argumentsStr = "/C net start mysql";
                    break;

                case DatabaseServiceOperation.StopMySql:
                    argumentsStr = "/C net stop mysql";
                    break;

                case DatabaseServiceOperation.StartRedis:
                    argumentsStr = "/C net start redis";
                    break;

                case DatabaseServiceOperation.StopRedis:
                    argumentsStr = "/C net stop redis";
                    break;

                case DatabaseServiceOperation.StartMongoDb:
                    argumentsStr = "/C net start mongodb";
                    break;

                case DatabaseServiceOperation.StopMongoDb:
                    argumentsStr = "/C net stop mongodb";
                    break;
            }

            return argumentsStr;
        }

        /// <summary>
        ///     分割线
        /// </summary>
        private static void PrintLine()
        {
Console.WriteLine(
"-----------------------------------------------------"); } } }

 

运行效果:

技术分享图片

技术分享图片

.NET以管理员身份运行程序

标签:数据   my sql   技术   rate   mon   operation   private   script   esc   

原文地址:https://www.cnblogs.com/LifeDecidesHappiness/p/8203592.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!