c#使用sc命令注册开发Windows服务
来源:互联网 发布:oracle12c连接数据库 编辑:程序博客网 时间:2024/06/09 18:27
Windows服务的开发如果中规中矩的话开发测试安装都十分繁琐,现备忘一个简易的方案。
主要原理也很简单,就是利用Windows自带的服务管理命令sc进行注册,运行。
using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Linq;using System.ServiceProcess;using System.Text;namespace wiffiMinitorSvr{ static class Program { static string excuteCmd(string cmd) { Console.WriteLine("will be excute command {0}", cmd); Process proc = new Process(); proc.StartInfo.FileName = "cmd.exe"; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardInput = false; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.CreateNoWindow = true; proc.StartInfo.Arguments = "/c " + cmd; proc.Start(); proc.WaitForExit(); string ret = proc.StandardOutput.ReadToEnd() + proc.StandardError.ReadToEnd(); Console.WriteLine(ret); return ret; } /// <summary> /// The main entry point for the application. /// </summary> static void Main(string[] args) { FileStream os = null; try { os = new FileStream("d:\\wiffiMonitor.log", FileMode.Create); Console.SetOut(new StreamWriter(os)); Console.WriteLine("log will begin --------"); } catch (Exception e) { Console.WriteLine(e); return; } string result = excuteCmd("sc qc wiffiMonitor"); if (result.IndexOf("1060") == -1) { try { Console.WriteLine("will be run service ..."); ServiceBase.Run(new ServiceBase[]{ new wiffiMinitorSvr() }); Console.WriteLine("serivce running ...."); } catch (Exception e) { Console.WriteLine(e); } if (null != os) os.Close(); return; } try { var path = Process.GetCurrentProcess().MainModule.FileName; string cmd = String.Format("sc create wiffiMonitor binPath= \"{0}\" DisplayName= wiffi助手 start= auto", path); excuteCmd(cmd); excuteCmd("sc start wiffiMonitor r"); excuteCmd("shutdown -r"); } catch (Exception e) { Console.WriteLine(e.ToString()); } if (null != os) os.Close(); } }}
0 0
- c#使用sc命令注册开发Windows服务
- 使用sc create 注册windows服务时候的坑
- SC命令配置服务(注册SVN服务)
- windows安装服务命令sc
- SC命令添加windows服务
- SC命令添加windows服务
- SC命令添加windows服务
- windows安装服务命令sc
- sc命令管理windows服务
- SC命令注册服务的方法
- WIN32服务----SC命令使用
- windows中服务命令sc的用法
- SC-控制Windows服务的命令
- 利用sc命令添加windows 服务
- SC命令(windows服务开启/禁用)
- SC命令创建和删除windows服务
- windows 使用sc命令将程序注册成服务 实现后台运行开机自启
- 使用SC命令行配置启动windows服务
- 学习的网址
- 【禅道】--不使用帮助的IT员工当不了好老板(实战篇)
- ios tableView那些事 (九) tableview的删除
- 初识ajax
- vb.net机房收费系统重构——总结(四)软件工程文档3
- c#使用sc命令注册开发Windows服务
- linux下/etc/rc.d目录的介绍及redhat启动顺序
- ios tableView那些事 (八) tableview的插入移动
- MyBatis的架构设计以及实例分析
- C++Building DayFOUR
- 文章标题
- sendcloud的api调用时可能出现的问题
- input file控件限制上传文件类型
- CF#304Div2