先看下代码
1 namespace UpdaterServer 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 if (args == null || args.Length == 0) 8 { 9 ServiceBase[] servicesToRun; 10 servicesToRun = new ServiceBase[] 11 { 12 new UpdateClientServer() 13 }; 14 ServiceBase.Run(servicesToRun); 15 } 16 else if (args.Length == 1) 17 { 18 switch (args[0]) 19 { 20 //安装并启动服务 21 case "1": 22 //取当前可执行文件路径 23 var path = Process.GetCurrentProcess().MainModule.FileName + ""; 24 Process.Start("sc", "create UpdateClientServer binpath= "" + path + "" displayName= UpdateClientServer "); 25 Process.Start("sc", "start UpdateClientServer"); 26 break; 27 case "2": 28 Process.Start("sc", "stop UpdateClientServer"); 29 Process.Start("sc", "delete UpdateClientServer"); 30 break; 31 default: 32 Console.WriteLine("[1]安装并启动服务 [2]停止并卸载服务 [其他不识别字符]帮助信息 "); 33 break; 34 } 35 } 36 } 37 } 38 }