参考来源:
https://www.jb51.net/article/32645.htm
https://www.cnblogs.com/appskyy/p/11019647.html (主要就是这个DOS CMD 调用DOS)
public class SystemServicesMSCHelper

    {
public static void StartServicesByServiceName(string servicesName)
{
using (ServiceController sc = new ServiceController(servicesName))
{
if (sc.CanStop)//判断是否可以关闭
{
try
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);//服务停止了才继续下一步 }
catch (Exception ex)
{
Console.WriteLine("停止服务失败:" + ex);
}
} if (!sc.CanStop)//判断是否可以开启
{
try
{
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);//服务运行了才继续下一步
}
catch (Exception ex)
{
Console.WriteLine("启动服务失败:" + ex);
}
}
sc.Close();
sc.Dispose();
} } private static string CmdPath = @"C:\Windows\System32\cmd.exe"; /// <summary>
/// 通过管理员命令运行CMD启动服务
/// </summary>
/// <param name="serverName"></param>
public static void RunCmdServicesByServerName(string serverName)
{
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
//判断当前登录用户是否为管理员
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
StartServicesByServiceName(serverName);
System.Diagnostics.Process.Start(CmdPath, "/c sc Start " + serverName);
}
else
{
//创建启动对象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = @"C:\Windows\System32\";
startInfo.FileName = CmdPath;
startInfo.Arguments = $"/c sc Start " + serverName;
//设置启动动作,确保以管理员身份运行
startInfo.Verb = "RunAs";
try
{
System.Diagnostics.Process.Start(startInfo);
}
catch
{ } }
} }

C# 启动windows 服务(通过cmd管理员方式和winform 方式)的更多相关文章

  1. 使用.NET Core中创建Windows服务(一) - 使用官方推荐方式

    原文:Creating Windows Services In .NET Core – Part 1 – The "Microsoft" Way 作者:Dotnet Core Tu ...

  2. 使用.NET Core创建Windows服务(一) - 使用官方推荐方式

    原文:使用.NET Core创建Windows服务(一) - 使用官方推荐方式 原文:Creating Windows Services In .NET Core – Part 1 – The &qu ...

  3. 网页启动Windows服务

    如何在网页启动Windows服务   由于公司有许多windows服务进行业务的处理,所谓对服务的维护也是一个比较头痛的问题,因为自己也不知道服务什么时候自动停了,而且更主要的原因是服务都是由运维部门 ...

  4. c#启动windows服务问题总结

    程序以管理员权限运行的原因 在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员权限登录,其应用程序默认情况下 ...

  5. 如何在网页启动Windows服务

    由于公司有许多windows服务进行业务的处理,所以对服务的维护也是一个比较头痛的问题,因为自己也不知道服务什么时候自动停了,而且更主要的原因是服务都是由运维部门在维护管理,开发这边没有直接操作服务的 ...

  6. Windows服务中用Timer和线程两种方式来执行定时任务

    在Service服务文件夹下新建Windows服务 - TestService

  7. 启动windows服务的bat文件编写格式

    1.bat文件需要和bin文件内容放在一起 启动服务的bat文件如下: sc create 邮件服务 binPath= "%~dp0可执行文件名称.exe" start= auto ...

  8. 启动Windows服务

    实现效果: 知识运用: ServiceController类的ServiceName Status属性 public string ServiceName {get; set;} //对此Servic ...

  9. Windows Service 学习系列(二):C# windows服务:安装、卸载、启动和停止Windows Service几种方式

    一.通过InstallUtil.exe安装.卸载.启动.停止Windows Service 方法一 1.以管理员身份运行cmd 2.安装windows服务 切换cd C:\Windows\Micros ...

  10. 无法从命令行或调试器启动服务,必须首先安装Windows服务(使用installutil.exe),然后用ServerExplorer、Windows服务器管理工具或NET START命令启动它

    无法从命令行或调试器启动服务,必须首先安装Windows服务(使用installutil.exe),然后用ServerExplorer.Windows服务器管理工具或NET START命令启动它 1. ...

随机推荐

  1. 【Unity3D】把相机视角放置到编辑器当前位置视角

    [Unity3D]把相机视角放置到编辑器当前位置视角 零.问题 Unity开发过程中,经常要将相机调整值对应位置,但是通过数值来调有时候不怎么方便,有什么方法可以快速让相机的视角与编辑器当前视角同步呢 ...

  2. MaxKB web 站点知识库选择器的花样玩法

    背景:MaxKB 创建知识库支持"web 站点"的这种形式,但是很多同学不知道怎么录入选择器来针对性的获取某一部分内容. 1. 选择器作用 选择器用于定位网页中特定的元素,以便获取 ...

  3. 使用Python对理想气体进行建模

    引言 在日常生活中,我们常常听到"气体",但你是否知道气体的行为是如何被科学家们用数学模型描述的呢?今天,我们就来聊聊如何用 Python 对理想气体进行建模,帮助大家更好地理解气 ...

  4. PriorityQueue作用和源码

    一.PriorityQueue作用和源码 PriorityQueue跟前几个阻塞队列不一样,并没有实现BlockingQueue接口,只是实现了Queue接口,Queue接口中定义了几组放数据和取数据 ...

  5. 搭建zookeeper集群常见的报错原因(总结)

    N.1 报错:Connection broken for id 2, my id = 3, error = java.io.IOException: Channel eof ... 原因:server ...

  6. Email邮箱验证码发送

    以下文件保存到/static/email.txt <!DOCTYPE html> <html lang="en" xmlns:th="http://ww ...

  7. 【工具】Typora中主题css修改|看了这篇,一劳永逸

    真正的指南 1. 查看当前的css shift+f12,与一般浏览器调试一样,先打开控制台,查找你需要修改的地方叫什么名字.(也可以点击"视图"-"开发者工具" ...

  8. 【经验】VMware|Win11的Ubuntu虚拟机启动虚拟化,报错此平台不支持虚拟化的 Intel VT-x/EPT(方案汇总+自己的解决方案)

    2024/04/24说明:这篇暂时修改为粉丝可见,因为正在冲粉丝量,等到我弄完了粉丝量的要求,我就改回来!不方便看到全文的小伙伴不好意思!! 我开虚拟化是为了在虚拟机中运行VirtualBox,如果不 ...

  9. java代码中启动exe程序最简单的方法

    使用awt的Desktop类的open方法: public static void startExe(String exePath){ try { if(StringUtils.isNotBlank( ...

  10. TensorFlow 基础 (02)

    前面对 tensorflow 的基础数据类型, 数值, 字符串, 布尔等, 有初步认识,尤其是重点的 tensor 张量, 包含了, 标量, 向量, 矩阵 ... 这样的基本概念. 我现在回过头来看, ...