zoukankan      html  css  js  c++  java
  • webform调用windows服务

    准备工作:
    0.电脑->管理->本地用户和组->组->Administrator双击->隶属->添加Network service->确定
    1.启动windows服务Windows Installer
    2.创建winform项目  WindowsFormsApplication1
    3.添加windows服务 service1
    4.添加代码
     protected override void OnStart(string[] args)
            {
                if (args != null && args.Length > 0)
                {
                    if (args[0] == "1")
                    {
                        string path = $@"d:kxbbbb{DateTime.Now.ToLongDateString()}.txt";
                        File.Create($"{path}");
                    }
                    else if (args[0] == "2")
                    {
                        string path = $@"d:kxqqq{DateTime.Now.ToLongDateString()}.txt";
                        File.Create($"{path}");
    
                    }
                }
    
                // TODO: 在此处添加代码以启动服务。
            }
    5.Main函数启动
    /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                //Application.EnableVisualStyles();
                //Application.SetCompatibleTextRenderingDefault(false);
                //Application.Run(new Form1());
                ServiceBase[] serviceRun;
                serviceRun = new ServiceBase[] { new Service1() };
                ServiceBase.Run(serviceRun);
            }
    
    
    6.service1.cs右键查看设计器,再右键添加安装程序,默认添加两个
    serviceInstaller1和serviceProcessInstaller1
    7.serviceInstaller1右键属性修改Description 为这是一个测试服务
    8.serviceProcessInstaller1右键属性 Account修改为NetworkService
    9.管理员打开cmd
    cd C:WindowsMicrosoft.NETFramework(64)v4.0.30319 把InstallUtil.exe复制到要发布的EXE的debug目录里面,命令切换等到该DEBUG目录
    10.安装服务
    InstallUtil.exe WindowsFormsApplication1.exe
    11.webform调用
     protected void Page_Load(object sender, EventArgs e)
            {
                ServiceController service = new ServiceController("Service1");
                //if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                //{
                //    service.Start();//打开服务 
                //}
                //停止服务
                service.Stop();//这行报错:无法打开计算机“.”上的 Service1 服务。
                service.WaitForStatus(ServiceControllerStatus.Stopped);
                //启动服务
                string[] args = { "2" };
                service.Start(args);
                service.WaitForStatus(ServiceControllerStatus.Running);
            }
  • 相关阅读:
    Guava学习笔记(4):Ordering犀利的比较器
    Guava学习笔记(3):复写的Object常用方法
    Guava学习笔记(1):Optional优雅的使用null
    [BZOJ1076][SCOI2008]奖励关
    [BZOJ1821][JSOI2010]部落划分
    [BZOJ1041]圆上的整点
    [Luogu2324]八数码难题
    [BZOJ1085][SCOI2005]骑士精神
    [BZOJ3109] [cqoi2013]新数独
    [LnOI2019]长脖子鹿省选模拟赛 东京夏日相会
  • 原文地址:https://www.cnblogs.com/kexb/p/6722527.html
Copyright © 2011-2022 走看看