zoukankan      html  css  js  c++  java
  • C#系统服务安装

    转载 http://blog.csdn.net/vvhesj/article/details/8349615

    1.1创建WindowsService项目

      导入需要的引用比如System.configuration、System.Web.Service(网站服务)、System.Windows.Forms(应用程序服务)、Newtonsoft.Json(json数据,这个很常用),按自己需要添加。

      添加app.config用于管理配置信息

    2.1添加安装程序

      打开Service.cs右键点击,添加安装程序,会自动添加安装程序类,保存下就行了。

      Service.cs的属性需要设置下  1)ServiceName 服务器名,最好用英文。2)StartType设置Automatic自动启动就好了。Disable禁用没人会用吧。Manual手动。

      ProcessInstaller.cs的属性需要设置下  1)Account设置LocalSystem  本地系统就行了。

                            LocalService    充当本地计算机上非特权用户的帐户,该帐户将匿名凭据提供给所有远程服务器。

                            LocalSystem    服务控制管理员使用的帐户,它具有本地计算机上的许多权限并作为网络上的计算机。

                            NetworkService    提供广泛的本地特权的帐户,该帐户将计算机的凭据提供给所有远程服务器。
                            User    由网络上特定的用户定义的帐户。如果为 ServiceProcessInstaller.Account 成员指定 User,

                                则会使系统在安装服务时提示输入有效的用户名和密码,

                                除非您为 ServiceProcessInstaller 实例的 Username 和 Password 这两个属性设置值。

    3.1编写代码

      Service.cs服务类写服务启动关闭定时运行的方法

      其他的按正常写代码一样添加类库,写需要的代码。

    4.1安装服务

      去bindebug下面找到service的EXE文件,复制到需要安装的地方,比如windowsSystem32之类的地方
      输入完整的debug目录下的InstallUtil.exe 空格 输入刚刚复制的service.exe的完整路径,回车进行安装

    4.2安装服务

      通过新建项目window安装包进行封装,这个就不讲解了

      http://blog.csdn.net/dyzcode/article/details/6981547 可以看这里

    4.3安装服务

      通过在ProcessInstaller.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_MACHINEServicesCurrentControlSet  
    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.   
    17.     try  
    18.     {  
    19.         //Let the project installer do its job  
    20.         base.Install(stateSaver);  
    21.   
    22.         //Open the HKEY_LOCAL_MACHINESYSTEM key  
    23.         system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");  
    24.         //Open CurrentControlSet  
    25.         currentControlSet = system.OpenSubKey("CurrentControlSet");  
    26.         //Go to the services key  
    27.         services = currentControlSet.OpenSubKey("Services");  
    28.         //Open the key for your service, and allow writing  
    29.         service = services.OpenSubKey(conServiceName, true);  
    30.         //Add your service's description as a REG_SZ value named "Description"  
    31.         service.SetValue("Description", "<span style="font-family:KaiTi_GB2312;">描述语言</span>");  
    32.         //(Optional) Add some custom information your service will use...  
    33.         config = service.CreateSubKey("Parameters");  
    34.     }  
    35.     catch (Exception e)  
    36.     {  
    37.         Console.WriteLine("An exception was thrown during service installation: " + e.ToString());  
    38.     }  
    39. }  
    40.   
    41. /// <summary>  
    42. /// 卸载服务  
    43. /// </summary>  
    44. /// <param name="savedState"></param>  
    45. public override void Uninstall(System.Collections.IDictionary savedState)  
    46. {  
    47.     Microsoft.Win32.RegistryKey system,  
    48.         currentControlSet,  
    49.         services,  
    50.         service;  
    51.   
    52.     try  
    53.     {  
    54.         //Drill down to the service key and open it with write permission  
    55.         system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");  
    56.         currentControlSet = system.OpenSubKey("CurrentControlSet");  
    57.         services = currentControlSet.OpenSubKey("Services");  
    58.         service = services.OpenSubKey(conServiceName, true);  
    59.         //Delete any keys you created during installation (or that your service created)  
    60.         service.DeleteSubKeyTree("Parameters");  
    61.         //...  
    62.     }  
    63.     catch (Exception e)  
    64.     {  
    65.         Console.WriteLine("Exception encountered while uninstalling service: " + e.ToString());  
    66.     }  
    67.     finally  
    68.     {  
    69.         //Let the project installer do its job  
    70.         base.Uninstall(savedState);  
    71.     }  

    代码添加完成后

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

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

    @echo off
    C:WINDOWSMicrosoft.NetFrameworkv2.0.50727InstallUtil.exe -i "WindowsService1.exe"
    @pause

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

    @echo off
    C:WINDOWSMicrosoft.NETFrameworkv2.0.50727InstallUtil.exe -u "WindowsService1.exe"
    @pause
    install.bat即完成安装。

  • 相关阅读:
    如何方便的下载youtube视频?
    88. Merge Sorted Array
    83. Remove Duplicates from Sorted List
    70. Climbing Stairs
    用opencv+python全屏进行显示图片
    58. Length of Last Word
    numpy的resize和reshape区别
    Opencv的绘图
    Python的slice问题
    jqgrid一次性加载
  • 原文地址:https://www.cnblogs.com/carlows/p/6846464.html
Copyright © 2011-2022 走看看