zoukankan      html  css  js  c++  java
  • Windows服务安装后自动启动

    在服务上添加安装程序,在serviceProcessInstaller1控件的Committed事件当中添加代码:

                System.ServiceProcess.ServiceController     controller     =     new     System.ServiceProcess.ServiceController("MyService");  
                controller.Start();  

    MyService为服务名


    此操作之前要先设置下两个控件

    设置serviceProcessInstaller1控件的Account属性为“LocalSystem
    设置serviceInstaller1控件的StartType属性为"Automatic"

    以上代码有误,待解决后再修改

    ----------------华丽的分割线--------------------------

    在服务器上添加安装程序,在private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)事件中,添加以下代码:

    Process p = new Process();
    p.StartInfo.FileName 
    = "cmd.exe";
    p.StartInfo.UseShellExecute 
    = false;
    p.StartInfo.RedirectStandardInput 
    = true;
    p.StartInfo.RedirectStandardOutput 
    = true;
    p.StartInfo.RedirectStandardError 
    = true;
    p.StartInfo.CreateNoWindow 
    = true;
    p.Start();
    string Cmdstring = "sc start myservice"//CMD命令
    p.StandardInput.WriteLine(Cmdstring);
    p.StandardInput.WriteLine(
    "exit");

    即可在安装服务后立刻启动windows服务

    注: 引用using System.Diagnostics;//调用Process

    以上代码,亲测成功! 欢迎点评(在网上找了n久解决方案,最后还是自己搞定,转载请注明出处)

  • 相关阅读:
    2004选拔赛 最小值
    [JSOI2008]最大数maxnumber
    HDOJ 2896 病毒侵袭
    POJ 2243 Knight Moves
    HDOJ Is It A Tree?
    MST 小希的迷宫
    PKU 3278 Catch That Cow
    POJ 2488 A Knight's Journey
    [SCOI2006]整数划分
    COJ 1259: 跳跳
  • 原文地址:https://www.cnblogs.com/wfwup/p/1375382.html
Copyright © 2011-2022 走看看