【教程】C#开启、关闭SQLServer服务

using System;
using System.Collections.Generic;
using System.Management;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            while (1 == 1)
            {
                Console.Write("nn 可选操作 : nn 1.启动数据库服务 2.停止数据库服务 nn 开始执行 : ");
                string strQueryState = Console.ReadLine();
                if (strQueryState.Trim() == "1")
                {
                    string strFilePath = GetFilePath("Start");
                    System.IO.File.AppendAllText(strFilePath, GetCmdStr("start"), System.Text.Encoding.GetEncoding("gb2312"));
                    System.Diagnostics.Process.Start(strFilePath);
                }
                else if (strQueryState.Trim() == "2")
                {
                    string strFilePath = GetFilePath("Stop");
                    System.IO.File.AppendAllText(strFilePath, GetCmdStr("stop"), System.Text.Encoding.GetEncoding("gb2312"));
                    System.Diagnostics.Process.Start(strFilePath);
                }
            }

        }

        /// <summary>
        /// 操作所需的Cmd指令
        /// </summary>
        /// <param name="StateStr">操作</param>
        /// <returns>Cmd指令</returns>
        private static string GetCmdStr(string StateStr)
        {
            string cmdStr = "";
            if (StateStr.ToLower().Trim() == "start")
            {
                cmdStr = @"@echo. & del %0 & @echo off & @echo.正在启动服务 MSSQLSERVER and MSSQLSERVERAGENT ...... & "
                        + "@echo. & @net start MSSQLSERVER & @net start SQLSERVERAGENT & @echo.服务启动完毕!";
            }
            else if (StateStr.ToLower().Trim() == "stop")
            {
                cmdStr = @"@echo. & del %0 & @echo off & @echo.正在停止服务 MSSQLSERVER and MSSQLSERVERAGENT ...... & "
                        + "@echo. & @net stop sqlserveragent &  @net stop mssqlserver & @echo.服务停止完毕!";
            }
            return cmdStr;
        }

        /// <summary>
        /// 获取一个文件是否存在于磁盘
        /// </summary>
        /// <param name="PathStr">文件名称</param>
        /// <returns>返回可创建文件路径</returns>
        private static string GetFilePath(string strFileName)
        {
            string strTem = "";//用于存储要保存文件路径
            string[] strPath = new string[] { @"C:\", @"D:\", @"E:\", @"F:\" };//检测的盘符
            strFileName += DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()
                + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString().Trim() + ".cmd";//为防止重复为文件重新命名

            foreach (string pathStr in strPath)//循环判断文件目录存在性
            {
                if (!System.IO.Directory.Exists(pathStr))//判断目录存在
                {
                    continue;
                }
                else
                {
                    if (System.IO.File.Exists(pathStr + strFileName))//判断文件存在
                    {
                        continue;
                    }
                    else
                    {
                        strTem = pathStr + strFileName;
                        break;
                    }
                }
            }
            return strTem;//返回可建文件路径
        }
    }
}