简体   繁体   English

如何从C#代码(Winforms)安装Windows Service

[英]How to install Windows Service from C# code (Winforms)

I have a Windows service developed in C# and I would like to install this service from a Winforms application in C# but not in a DOS command: 我有一个用C#开发的Windows服务,我想从C#中的Winforms应用程序安装此服务,但不能在DOS命令中安装:

installutil \i ..  

Is it possible? 可能吗?

Installing a Windows Service requires admin privileges so the winforms app would need to be running with elevated privileges. 安装Windows服务需要管理员权限,因此winforms应用程序需要以提升的权限运行。 If this is ok, then you could use Process.Start to invoke the installutil.exe . 如果可以,则可以使用Process.Start调用installutil.exe Process.Start also allows you to pass in a username and password so that is another option if the winforms app does not have elevated permissions. Process.Start还允许您传递用户名和密码,因此,如果winforms应用程序没有提升的权限,则这是另一种选择。

Have a look at this article which has more information https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.createnowindow%28v=vs.110%29.aspx 看看这篇文章,了解更多信息https://msdn.microsoft.com/zh-cn/library/system.diagnostics.processstartinfo.createnowindow%28v=vs.110%29.aspx

Installing a service requires you to excute two steps: 安装服务需要您执行两个步骤:

  • install them via installutil.exe /i filename.exe 通过installutil.exe /i filename.exe安装它们
  • then starting them with the services.msc service starter. 然后使用services.msc服务启动器启动它们。

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

相关问题 如何在 C# 中以编程方式安装 Windows 服务? - How to install a windows service programmatically in C#? C#COMException作为Windows服务,但WinForms可以 - C# COMException as Windows Service, but WinForms OK 是否可以通过调用AssemblyInstaller来安装用C ++编写的Windows服务(EXE)(使用CreateService WINAPI)。从C#代码安装? - Is it possible to install a windows service(EXE) written in C++(using CreateService WINAPI) by calling assemblyInstaller.Install from a C# code? 在C#Winforms中从Consul消费服务 - Consuming Service From Consul in c# Winforms 如何在 ASP.NET 核心 MVC 中安装使用 C# Winforms 制作的现有 web 服务 - How to install an existing web service made with C# Winforms in ASP.NET Core MVC C#如何为Windows服务创建安装项目以自动安装它? - C# How to create a Setup project for a Windows Service to install it automagically? C#如何在应用程序安装过程中安装Windows Service - C# How to install Windows Service as part of application installation 如何在C#中安装Windows服务(通过installutil)? - How to install a Windows service (via installutil) in C#? 如何在远程服务器上安装C#Windows服务? - How to install a C# Windows Service on a remote server? 从C#Winforms执行JavaScript代码 - Executing JavaScript code from C# Winforms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM