c#console app as service

时间:2016-11-03 01:07:13

标签: service cmd

只是一个简单的consloe应用程序,我想在任何时候都作为服务运行,没有VB模板。

到目前为止,我有:

  • 在清单中给出了highestAvailable权限,并在属性中绑定了清单
  • 右键单击exe兼容性并选择"以管理员身份运行"
  • 打开安全设置,以便"所有人",管理员,服务,系统,网络服务,本地服务和一些用户都拥有完全访问权限。
  • 使用此代码成功添加到服务列表,代码从单独的解决方案表单运行,也运行清单和管理员:

    private static void cmd_PROMPT(string cmd)
    {
        ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
        cmdStartInfo.FileName = @"C:\Windows\System32\cmd.exe";
        cmdStartInfo.RedirectStandardOutput = true;
        cmdStartInfo.RedirectStandardError = true;
        cmdStartInfo.RedirectStandardInput = true;
        cmdStartInfo.Verb = "runas";
        cmdStartInfo.UseShellExecute = false;
        cmdStartInfo.CreateNoWindow = true;
        cmdStartInfo.Arguments = "/user:Administrator ";
    
        Process cmdProcess = new Process();
        cmdProcess.StartInfo = cmdStartInfo;
        cmdProcess.ErrorDataReceived += cmd_Error;
        cmdProcess.OutputDataReceived += cmd_DataReceived;
        cmdProcess.EnableRaisingEvents = true;
        cmdProcess.Start();
        cmdProcess.BeginOutputReadLine();
        cmdProcess.BeginErrorReadLine();
    
        cmdProcess.StandardInput.WriteLine(cmd);    
        MessageBox.Show(">>" + cmd);
        //cmdProcess.WaitForExit();
    }
    
    
    private void button3_Click(object sender, EventArgs e)
    {
        string s;
        string prgmName = "@QU@Service";
        string prgmPath = "c:\\@QU@\\aquaService.exe";
        string prgmMode = "auto"; 
        s = "sc create " + prgmName + " binpath= " + prgmPath + " DisplayName= \"" + prgmName + "\" start= " + prgmMode;
    
        cmd_PROMPT(s);
    }
    

应用程序已进入控制面板中的服务列表,但它已停止" ...当我点击...动作...启动服务时,它会尝试但失败并显示{{ 1}}。
当我使用CMD指令时,它变为"开始"在服务列表中,但后来又回到停止:

error:1053 did not start in timely fashion

1 个答案:

答案 0 :(得分:0)

ty Noodles,有了你的线索我能够使这段代码正常工作:

    private void button3_Click(object sender, EventArgs e)
    {
        string s;
        string prgmName = "@QU@Service";
        string prgmPath = "c:\\@QU@\\aquaService.exe";
        string prgmMode = "auto";
        string _subKey = "Parameters";
        string keyName = "Application";
        RegistryKey _baseRegistryKey;
        s = "C:\\@QU@\\instsrv.exe " + prgmName + " C:\\@QU@\\srvany.exe";
        cmd_PROMPT(s);
        s = "sc config " + prgmName + " start=auto ";   //   boot 
        cmd_PROMPT(s);
        try
        {
            _baseRegistryKey = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\services", true);
            var sk1 = _baseRegistryKey.CreateSubKey(prgmName);
            _baseRegistryKey = sk1; sk1 = _baseRegistryKey.CreateSubKey(_subKey); 
            if (sk1 != null) { sk1.SetValue(keyName.ToUpper(), prgmPath, RegistryValueKind.String); }
        }
        catch (Exception ee)
        {
            MessageBox.Show(" " + ee, "Administrator");
        }              
        Thread.Sleep(5000);
        prgmName = "@QU@Service";
        prgmPath = "c:\\@QU@\\aquaService.exe";
        prgmMode = "auto";
        s = "sc start " + prgmName;
        cmd_PROMPT(s);    
    }