• 使用 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

     

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

  • 相关阅读:
    利用单臂路由实现VLAN间路由
    理解Hybrid接口的应用
    配置Trunk接口
    VLAN 基础设置及Aceess接口
    NFS实现文件共享
    Apache服务(基于IP地址,主机名,端口号)
    使用Vsftpd服务(匿名访问模式、本地用户模式)
    移动端响应式js代码
    快速搜索匹配插件
    无缝滚动插件
  • 原文地址:https://www.cnblogs.com/zencorn/p/1718767.html
Copyright © 2020-2023  润新知