-
卸载很简单,打开cmd, 直接输入 sc delete WinServiceTest即可
其余安装方式:
装Winfows服务首先要添加安装程序,添加安装程序步骤以下:
一、将Windows服务程序切换到设计视图, 右击设计视图选择“添加安装程序”
二、切换到刚被添加的ProjectInstaller的设计视图
通常设置以下:
设置serviceInstaller1组件的属性: 1) ServiceName = 服务名称 2) StartType = Automatic ,即自动 设置serviceProcessInstaller1组件的属性 1) Account = LocalSystem,帐户通常设置为本地系统
三、生成解决方案
安装服务:
方法1、使用DOS命令安装window服务
一、在服务所在的文件夹下的bin\debug文件夹下找到.exe文件(例如WindowsService1.exe)
将此文件拷贝到你想安装的文件夹中。
二、进入DOS界面
(VS2008-->Visual Studio Tools-->Visual Studio 2008 命令提示)来进入DOS,直接用cmd可能有些命令找不到;
三、输入
方法2、使用安装项目安装windows服务
我的比较推荐这个方法,选择目录安装更灵活,并且不用在DOS环境下运行。
由于本人比较懒,直接给出别人总结的地址
http://blog.csdn.net/dyzcode/article/details/6981547
注意,之后每次服务项目有更改的时候,须要编译服务后,在安装项目中刷新依赖项!!!
方法3、
在ProjectInstaller.cs的后台代码中添加安装服务和卸载服务的代码
- /// <summary>
- /// 安装服务
- /// </summary>
- /// <param name="stateSaver"></param>
- public override void Install(System.Collections.IDictionary stateSaver)
- {
- Microsoft.Win32.RegistryKey system,
- //HKEY_LOCAL_MACHINE\Services\CurrentControlSet
- currentControlSet,
- //...\Services
- services,
- //...\<Service Name>
- service,
- //...\Parameters - this is where you can put service-specific configuration
- config;
- try
- {
- //Let the project installer do its job
- base.Install(stateSaver);
- //Open the HKEY_LOCAL_MACHINE\SYSTEM key
- system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
- //Open CurrentControlSet
- currentControlSet = system.OpenSubKey("CurrentControlSet");
- //Go to the services key
- services = currentControlSet.OpenSubKey("Services");
- //Open the key for your service, and allow writing
- service = services.OpenSubKey(conServiceName, true);
- //Add your service's description as a REG_SZ value named "Description"
- service.SetValue("Description", "<span style="font-family: KaiTi_GB2312;">描述语言</span>");
- //(Optional) Add some custom information your service will use...
- config = service.CreateSubKey("Parameters");
- }
- catch (Exception e)
- {
- Console.WriteLine("An exception was thrown during service installation:\n" + e.ToString());
- }
- }
- /// <summary>
- /// 卸载服务
- /// </summary>
- /// <param name="savedState"></param>
- public override void Uninstall(System.Collections.IDictionary savedState)
- {
- Microsoft.Win32.RegistryKey system,
- currentControlSet,
- services,
- service;
- try
- {
- //Drill down to the service key and open it with write permission
- system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
- currentControlSet = system.OpenSubKey("CurrentControlSet");
- services = currentControlSet.OpenSubKey("Services");
- service = services.OpenSubKey(conServiceName, true);
- //Delete any keys you created during installation (or that your service created)
- service.DeleteSubKeyTree("Parameters");
- //...
- }
- catch (Exception e)
- {
- Console.WriteLine("Exception encountered while uninstalling service:\n" + e.ToString());
- }
- finally
- {
- //Let the project installer do its job
- base.Uninstall(savedState);
- }
- }
/// <summary> /// 安装服务 /// </summary> /// <param name="stateSaver"></param> public override void Install(System.Collections.IDictionary stateSaver) { Microsoft.Win32.RegistryKey system, //HKEY_LOCAL_MACHINE\Services\CurrentControlSet currentControlSet, //...\Services services, //...\<Service Name> service, //...\Parameters - this is where you can put service-specific configuration config; try { //Let the project installer do its job base.Install(stateSaver); //Open the HKEY_LOCAL_MACHINE\SYSTEM key system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System"); //Open CurrentControlSet currentControlSet = system.OpenSubKey("CurrentControlSet"); //Go to the services key services = currentControlSet.OpenSubKey("Services"); //Open the key for your service, and allow writing service = services.OpenSubKey(conServiceName, true); //Add your service's description as a REG_SZ value named "Description" service.SetValue("Description", "描述语言"); //(Optional) Add some custom information your service will use... config = service.CreateSubKey("Parameters"); } catch (Exception e) { Console.WriteLine("An exception was thrown during service installation:\n" + e.ToString()); } } /// <summary> /// 卸载服务 /// </summary> /// <param name="savedState"></param> public override void Uninstall(System.Collections.IDictionary savedState) { Microsoft.Win32.RegistryKey system, currentControlSet, services, service; try { //Drill down to the service key and open it with write permission system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System"); currentControlSet = system.OpenSubKey("CurrentControlSet"); services = currentControlSet.OpenSubKey("Services"); service = services.OpenSubKey(conServiceName, true); //Delete any keys you created during installation (or that your service created) service.DeleteSubKeyTree("Parameters"); //... } catch (Exception e) { Console.WriteLine("Exception encountered while uninstalling service:\n" + e.ToString()); } finally { //Let the project installer do its job base.Uninstall(savedState); } }
代码添加完成后
添加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即完成安装。