本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!