从C#应用程序安装TopShelf服务



嗨,我正在尝试从c#中的Windows窗体应用程序安装一个服务(使用TopShelf创建)。我曾考虑使用Service Manager类,但这似乎不是一个选项。因此,我考虑使用Process类来调用安装。通常要安装Topshelf服务,您需要运行以下程序:

MyService.EXE安装-someOptions

然而,当我在过程中尝试此操作时

 var servicePath = @"C:Program Files (x86)Blah ServicesbinGatewayGateway.exe";
 var userName = "test";
 var password = "Word!";
 
 try{
      Process proc = new Process(); //call new Process
        ProcessStartInfo info = new ProcessStartInfo(); //call new ProcessStartInfo
        info.Arguments = "install -instance:default -username:" + userName + "-password:" + password;
        info.FileName = servicePath; //set the file name (location)
        proc.StartInfo = info; //put the StartInfo into the Procces method
        proc.Start(); //Start the procces (sc.exe with the arguments)
        proc.WaitForExit(); //waits till the procces is don
     
   }

我甚至想过使用一个命令窗口,用以下内容替换Arguments和File名称:

   info.Arguments = "//k " + servicePath +" install -instance:default -username:" + userName + "-password:" + password;
            info.FileName = "cmd.exe"; //set the file name (location)

不过,两者似乎都不起作用。有人有什么想法吗?

只是尝试一下,但您是以管理员身份运行WinForms应用程序(或Visual Studio)吗?作为管理员,我一直在使用以下内容来启动TopShelf安装:

System.Diagnostics.Process.Start(@"C:winServicesextractsbinservice.exe", "install");

最新更新