当前位置:   article > 正文

安装Windows服务几种方法

安装windows服务

  安装Winfows服务首先要添加安装程序,添加安装程序步骤如下:

1、将Windows服务程序切换到设计视图, 右击设计视图选择“添加安装程序”

2、切换到刚被添加的ProjectInstaller的设计视图


一般设置如下:

 设置serviceInstaller1组件的属性:
    1) ServiceName = 服务名称
    2) StartType = Automatic ,即自动
 设置serviceProcessInstaller1组件的属性
    1) Account = LocalSystem,账户一般设置为本地系统

3、生成解决方案


安装服务:

方法一、使用DOS命令安装window服务

1、在服务所在的文件夹下的bin\debug文件夹下找到.exe文件(例如WindowsService1.exe)

将此文件拷贝到你想安装的文件夹中。

2、进入DOS界面

(VS2008-->Visual Studio Tools-->Visual Studio 2008 命令提示)来进入DOS,直接用cmd可能有些命令找不到;

3、输入


方法二、使用安装项目安装windows服务

个人比较推荐这个方法,选择目录安装更灵活,而且不用在DOS环境下运行。

因为本人比较懒,直接给出别人总结的地址

http://blog.csdn.net/dyzcode/article/details/6981547

注意,以后每次服务项目有更改的时候,需要编译服务后,在安装项目中刷新依赖项!!!


方法三、

ProjectInstaller.cs的后台代码中添加安装服务和卸载服务的代码

  1. /// <summary>
  2. /// 安装服务
  3. /// </summary>
  4. /// <param name="stateSaver"></param>
  5. public override void Install(System.Collections.IDictionary stateSaver)
  6. {
  7. Microsoft.Win32.RegistryKey system,
  8. //HKEY_LOCAL_MACHINE\Services\CurrentControlSet
  9. currentControlSet,
  10. //...\Services
  11. services,
  12. //...\<Service Name>
  13. service,
  14. //...\Parameters - this is where you can put service-specific configuration
  15. config;
  16. try
  17. {
  18. //Let the project installer do its job
  19. base.Install(stateSaver);
  20. //Open the HKEY_LOCAL_MACHINE\SYSTEM key
  21. system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
  22. //Open CurrentControlSet
  23. currentControlSet = system.OpenSubKey("CurrentControlSet");
  24. //Go to the services key
  25. services = currentControlSet.OpenSubKey("Services");
  26. //Open the key for your service, and allow writing
  27. service = services.OpenSubKey(conServiceName, true);
  28. //Add your service's description as a REG_SZ value named "Description"
  29. service.SetValue("Description", "描述语言");
  30. //(Optional) Add some custom information your service will use...
  31. config = service.CreateSubKey("Parameters");
  32. }
  33. catch (Exception e)
  34. {
  35. Console.WriteLine("An exception was thrown during service installation:\n" + e.ToString());
  36. }
  37. }
  38. /// <summary>
  39. /// 卸载服务
  40. /// </summary>
  41. /// <param name="savedState"></param>
  42. public override void Uninstall(System.Collections.IDictionary savedState)
  43. {
  44. Microsoft.Win32.RegistryKey system,
  45. currentControlSet,
  46. services,
  47. service;
  48. try
  49. {
  50. //Drill down to the service key and open it with write permission
  51. system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
  52. currentControlSet = system.OpenSubKey("CurrentControlSet");
  53. services = currentControlSet.OpenSubKey("Services");
  54. service = services.OpenSubKey(conServiceName, true);
  55. //Delete any keys you created during installation (or that your service created)
  56. service.DeleteSubKeyTree("Parameters");
  57. //...
  58. }
  59. catch (Exception e)
  60. {
  61. Console.WriteLine("Exception encountered while uninstalling service:\n" + e.ToString());
  62. }
  63. finally
  64. {
  65. //Let the project installer do its job
  66. base.Uninstall(savedState);
  67. }
  68. }
代码添加完成后

添加window service安装的批处理命令

1)在项目添加一个文本文件,更名为install.bat,编辑文件的内容如下:

@echo off
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe -i "WindowsService1.exe"
@pause

2)在项目添加一个文本文件,更名为uninstall.bat,编辑文件的内容如下

@echo off
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe -u "WindowsService1.exe"
@pause

明:上面绿色字体为服务名称

编译完成后将debug的文件拷贝到想安装的目录下,点击install.bat即完成安装。

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/981436
推荐阅读
相关标签
  

闽ICP备14008679号