简体   繁体   English

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

[英]Install TopShelf service from C# Application

Hi I am trying to install a service (created with TopShelf) from a Windows Forms Application, in c#. 嗨,我正在尝试使用c#从Windows窗体应用程序安装服务(使用TopShelf创建)。 I was looking at using the Service Manager Class but this doesn't seem to be an option. 我一直在使用Service Manager Class,但这似乎不是一个选择。 I therefore Was thinking of using the Process class to invoke the install. 因此,我正在考虑使用Process类来调用安装。 usually to install a Topshelf service you run the following : 通常要安装Topshelf服务,请运行以下命令:

MyService.EXE install -someOptions MyService.EXE安装-someOptions

However when I try this within a Process 但是,当我在流程中尝试此操作时

  var servicePath = @"C:\\Program Files (x86)\\Blah Services\\bin\\Gateway\\Gateway.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 } 

I even thought about using a command window and replacing the Arguments and File name with the following: 我什至考虑过使用命令窗口并将参数和文件名替换为以下内容:

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

Neither seem to be working though. 似乎都没有工作。 Has anyone got any ideas? 有人知道吗?

Just a stab at it, but are you running your WinForms app (or Visual Studio) as an administrator? 只是一个刺,但是您是否以管理员身份运行WinForms应用程序(或Visual Studio)? I have been using the following to fire TopShelf installations as an admin: 我一直在使用以下内容以管理员身份启动TopShelf安装:

System.Diagnostics.Process.Start(@"C:\winServices\extracts\bin\service.exe", "install");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM