C#打开Wi-Fi服务,C# WlanSvc
2021/10/13 11:44:20
本文主要是介绍C#打开Wi-Fi服务,C# WlanSvc,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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; } } }
这篇关于C#打开Wi-Fi服务,C# WlanSvc的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具