C#打开Wi-Fi服务,C# WlanSvc

互联网 2021/10/13 11:44:20

using System; using System.Security.Principal; using System.ServiceProcess; using System.Windows.Forms;namespace windowsServiceCTRL {public static class ServicesHelper{/// <summary> /// 判断是否安装了某个服务 /// </summary> /// <para…
using System;
using System.Security.Principal;
using System.ServiceProcess;
using System.Windows.Forms;

namespace windowsServiceCTRL
{
    public static class ServicesHelper
    {
        /// <summary>  
        /// 判断是否安装了某个服务  
        /// </summary>  
        /// <param name="serviceName"></param>  
        /// <returns></returns>  
        public static bool ISWindowsServiceInstalled(string serviceName)
        {
            try
            {
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController service in services)
                {
                    if (service.ServiceName.Trim().ToUpper().Equals(serviceName.Trim().ToUpper()))
                    {
                        return true;
                    }
                }                
                return false;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 确定当前主体是否属于具有指定 Administrator 的 Windows 用户组
        /// </summary>
        /// <returns>如果当前主体是指定的 Administrator 用户组的成员,则为 true;否则为 false。</returns>
        public static bool IsAdministrator()
        {
            bool result;
            try
            {
                WindowsIdentity identity = WindowsIdentity.GetCurrent();
                WindowsPrincipal principal = new WindowsPrincipal(identity);
                result = principal.IsInRole(WindowsBuiltInRole.Administrator);

                //http://www.cnblogs.com/Interkey/p/RunAsAdmin.html
                //AppDomain domain = Thread.GetDomain();
                //domain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
                //WindowsPrincipal windowsPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
                //result = windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
            }
            catch
            {
                result = false;
            }
            return result;
        }

        /// <summary>  
        /// 启动某个服务,以管理员身份运行
        /// </summary>  
        /// <param name="serviceName"></param>  
        public static bool StartService(string serviceName)
        {
            if(IsAdministrator() == false)
            {
                startAsAdmin();
                return false;
            }
            bool ret = false;
            try
            {
                ServiceController[] services = ServiceController.GetServices();


                foreach (ServiceController service in services)
                {
                    if (service.ServiceName.Trim().ToUpper().Equals(serviceName.Trim().ToUpper()))
                    {
                        service.Start();
                        service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                        break;
                    }
                }
                ret = true;
            }
            catch {
                ret = false;
            }
            return ret;
        }

        private static void startAsAdmin()
        {
            //创建启动对象
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            //设置运行文件
            startInfo.FileName = Application.ExecutablePath;
            //设置启动动作,确保以管理员身份运行
            startInfo.Verb = "runas";
            //如果不是管理员,则启动UAC
            System.Diagnostics.Process.Start(startInfo);
            //退出
            Environment.Exit(0);
        }

        /// <summary>  
        /// 启动某个服务,以管理员身份运行
        /// </summary>  
        /// <param name="serviceName"></param>  
        public static bool StartServiceCMD(string serviceName)
        {
            try
            {
                using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
                {
                    proc.StartInfo.CreateNoWindow = true;
                    proc.StartInfo.FileName = "cmd.exe";
                    proc.StartInfo.UseShellExecute = false;
                    proc.StartInfo.RedirectStandardError = true;
                    proc.StartInfo.RedirectStandardInput = true;
                    proc.StartInfo.RedirectStandardOutput = true;
                    proc.Start();
                    proc.StandardInput.WriteLine("net start " + serviceName);
                    proc.WaitForExit(1000);
                    proc.Close();
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Start CMD Error:" + ex.Message);
                return false;
            }
        }

        /// <summary>  
        /// 停止某个服务  
        /// </summary>  
        /// <param name="serviceName"></param>  
        public static bool StopService(string serviceName)
        {
            bool ret = false;
            try
            {
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController service in services)
                {
                    if (service.ServiceName.Trim().ToUpper().Equals(serviceName.Trim().ToUpper()))
                    {
                        service.Stop();
                        service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                        break;
                    }
                }
                ret = true;
            }
            catch {
                ret = false;
            }
            return ret;
        }

        /// <summary>  
        /// 判断某个服务是否启动  
        /// </summary>  
        /// <param name="serviceName"></param>  
        public static bool ISStart(string serviceName)
        {
            bool result = true;
            try
            {
                ServiceController[] services = ServiceController.GetServices();


                foreach (ServiceController service in services)
                {
                    if (service.ServiceName.Trim().ToUpper().Equals(serviceName.Trim().ToUpper()))
                    {
                        if ((service.Status == ServiceControllerStatus.Stopped)
                            || (service.Status == ServiceControllerStatus.StopPending))
                        {
                            result = false;
                            break;
                        }
                    }
                }
            }
            catch {
                result = false;
            }
            return result;
        }
    }
}

随时随地学软件编程-关注百度小程序和微信小程序
关于找一找教程网

本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。
本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。
[C#打开Wi-Fi服务,C# WlanSvc]http://www.zyiz.net/tech/detail-240811.html

上一篇:【C#】【Demo】.net使用GDI绘图帮助类

下一篇:C#WPF (DataGid学习)

赞(0)
关注微信小程序
程序员编程王-随时随地学编程

扫描二维码或查找【程序员编程王】

可以随时随地学编程啦!

技术文章导航 更多>
扫一扫关注最新编程教程