c# 服务安装后自动启动

switch (rs)
            {
                case 1:
                  
                    var path = @"e:\WindowsServiceTest.exe" + " s";
                    Process.Start("sc", "create myserver binpath= \"" + path + "\" displayName= StrongunionService start= auto");

                    Process p = new Process();
                    p.StartInfo.FileName = "cmd.exe";
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardInput = true;
                    p.StartInfo.RedirectStandardOutput = false;
                    p.StartInfo.RedirectStandardError = true;
                    p.StartInfo.CreateNoWindow = true;
                    p.Start();
                    string Cmdstring = "sc start myserver"; //CMD命令
                    p.StandardInput.WriteLine(Cmdstring);
                    Console.WriteLine("安装成功");
                    Console.Read();
                    break;
                case 2:
                    Process p2 = new Process();
                    p2.StartInfo.FileName = "cmd.exe";
                    p2.StartInfo.UseShellExecute = false;
                    p2.StartInfo.RedirectStandardInput = true;
                    p2.StartInfo.RedirectStandardOutput = true;
                    p2.StartInfo.RedirectStandardError = true;
                    p2.StartInfo.CreateNoWindow = true;
                    p2.Start();
                    string Cmdstring2 = "sc stop myserver"; //CMD命令
                    p2.StandardInput.WriteLine(Cmdstring2);
                    Process.Start("sc", "delete myserver");
                    Console.WriteLine("卸载成功");
                    Console.Read();
                    break;
                case 3:
                    
                    Console.WriteLine("卸载成功000");
                    Console.Read();
                    break;

            }

posted on 2017-04-26 15:30  邬兴亮  阅读(404)  评论(0编辑  收藏  举报

导航