zoukankan      html  css  js  c++  java
  • 使用 Windows service 访问域管理

    Scenario : Server端注册一个windows服务,此服务定时更新域内各台机器的注册表消息并更新至目标数据库信息。

    1创建windows service 项目,各内置属性代码如下。

     

    1. public partial class Service1 : ServiceBase

    2.     {

    3.         private System.Timers.Timer SeviceTimer;

    4.  

    5.         public Service1()

    6.         {

    7.             InitializeComponent();

    8.             this.SeviceTimer = new System.Timers.Timer();

    9.             this.SeviceTimer.Interval = 5*60*1000;

    10.             this.SeviceTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.SeviceTimer_Elapsed);

    11.             this.SeviceTimer.Enabled = false;

    12.         }

    13.  

    14.         protected override void OnStart(string[] args)

    15.         {

    16.             // TODO: Add code here to start your service.

    17.             this.SeviceTimer.Enabled = true;

    18.             this.LogMessage(System.DateTime.Now.ToString()+" Service Start.");

    19.             //RunBatFile();

    20.         }

    21.  

    22.         private void SeviceTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

    23.         {

    24.  

    25.  

    26.             if (System.IO.File.Exists(@"E:\David\QADashboardServices\UpdateApplication\UpdateGenServicePort.bat"))

    27.             {

    28.                 RunBatFile();

    29.                 this.LogMessage(System.DateTime.Now.ToString() + "   service update successfully !");

    30.             }

    31.             else

    32.             {

    33.                 this.LogMessage(System.DateTime.Now.ToString() + "    No File Exist !");

    34.             }

    35.  

    36.             

    37.         }

    38.  

    39.         protected override void OnStop()

    40.         {

    41.             // TODO: Add code here to perform any tear-down necessary to stop your service.

    42.             this.LogMessage(System.DateTime.Now.ToString() + " Service End . ");

    43.         }

    44.         private void LogMessage(string sMessage)

    45.         {

    46.  

    47.             FileStream fs = File.Create("C:\\ServicePort.txt");

    48.             StreamWriter sw = new StreamWriter(fs);

    49.             sw.WriteLine(sMessage);

    50.  

    51.             sw.Flush();

    52.             sw.Close();

    53.         }

    54.  

    55.         private void RunBatFile()

    56.         {

    57.             Process p = new Process();

    58.             p.StartInfo.UseShellExecute = false;

    59.             p.StartInfo.CreateNoWindow = false;//

    60.             p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

    61.             p.StartInfo.FileName = @"E:\David\QADashboardServices\UpdateApplication\UpdateGenServicePort.bat";

    62.             p.Start();

    63.             p.WaitForExit();  

    64.         }

    65.         }

    2 被调用的功能由批处理命令行文件完成[如何获取域内机器注册表消息见上篇文章]

       E:\David\QADashboardServices\UpdateApplication\UpdateGenServicePortConsoleApplication.exe

     

    3 生成ServiceInstaller Account 属性要设置成 LocalService,以便在Start Service时可以成功的用个人域内账号启动服务。

        

    4 项目文件目录下注册服务。

                  E:\David\QADashboardServices\GenQADashWindowsService\GenQADashWindowsService\bin\Debug>installutil.exe GenQADashWindowsService.exe

     

    开启服务时,选择域内目录,及启动账号,服务方能正常启动并有权限执行域内访问的功能。

  • 相关阅读:
    体检套餐管理系统
    Altium Designer9.4局域网内冲突的问题
    关于ARM Linux下的SD卡及U盘的挂载问题
    mkimage command not found – U-Boot images will not be built
    Xilinx------BUFG,IBUFG,BUFGP,IBUFGDS等含义以及使用
    linux文件压缩解压命令
    VIVADO 入门之仿真与逻辑分析仪使用
    Linux自动运行应用程序
    ZYNQ学习之二-EMIO
    inux grep 命令 搜索含有"zynq"字符的文件
  • 原文地址:https://www.cnblogs.com/zencorn/p/1718767.html
Copyright © 2011-2022 走看看