您的位置:首页 > 编程语言 > C#

使用C#代码方式创建、启动、停止、卸载Windows服务

2020-04-25 08:40 1546 查看

安装、启动一个Windows服务

[code]Console.WriteLine("创建 " + WindowsService.ServiceName);
var info = new ProcessStartInfo("sc", string.Format("create {0} binpath= \"{1}\" displayname= \"{2}\" start= auto type= own",
WindowsService.ServiceName, Process.GetCurrentProcess().MainModule.FileName, WindowsService.ServiceName))
{
WindowStyle = ProcessWindowStyle.Minimized,
UseShellExecute = false
};
var pro = Process.Start(info);
pro.WaitForExit();
Console.WriteLine("启动 " + WindowsService.ServiceName);
info.Arguments = "start " + WindowsService.ServiceName;
pro = Process.Start(info);
pro.WaitForExit();

停止、卸载一个Windows服务

[code]Console.WriteLine("停止 " + WindowsService.ServiceName);
var info = new ProcessStartInfo("sc", "stop " + WindowsService.ServiceName)
{
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = 独立窗口
};
var pro = Process.Start(info);
pro.WaitForExit();
Console.WriteLine("卸载 " + WindowsService.ServiceName);
info.Arguments = "delete " + WindowsService.ServiceName;
pro = Process.Start(info);
pro.WaitForExit();

来源页面:https://bbs.csdn.net/topics/392327136?page=1

  • 点赞
  • 收藏
  • 分享
  • 文章举报
黑石雨 发布了8 篇原创文章 · 获赞 2 · 访问量 878 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐