博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
webform调用windows服务
阅读量:7098 次
发布时间:2019-06-28

本文共 2151 字,大约阅读时间需要 7 分钟。

准备工作:0.电脑->管理->本地用户和组->组->Administrator双击->隶属->添加Network service->确定1.启动windows服务Windows Installer2.创建winform项目  WindowsFormsApplication13.添加windows服务 service14.添加代码 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函数启动///         /// 应用程序的主入口点。        ///         [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和serviceProcessInstaller17.serviceInstaller1右键属性修改Description 为这是一个测试服务8.serviceProcessInstaller1右键属性 Account修改为NetworkService9.管理员打开cmdcd C:\Windows\Microsoft.NET\Framework(64)\v4.0.30319 把InstallUtil.exe复制到要发布的EXE的debug目录里面,命令切换等到该DEBUG目录10.安装服务InstallUtil.exe WindowsFormsApplication1.exe11.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);        }

 

转载于:https://www.cnblogs.com/kexb/p/6722527.html

你可能感兴趣的文章
python文件操作
查看>>
oracle 建立表空间和用户
查看>>
nginx之 [error] 6702#0:XXX is forbidden (13: Permission denied)
查看>>
Maven属性(properties)标签的使用
查看>>
闲谈CDN网络架构
查看>>
dispatchEvent发送事件
查看>>
Java程序员金三银四精心准备的面试题及答案(基础篇)
查看>>
FileInputStreamTest
查看>>
u-boot 之配置分析 (2)
查看>>
对抗海量表格数据,【华为2012实验室】没有选择复仇者联盟
查看>>
JavaScript高级程序设计:第一章
查看>>
基础才是重中之重~你是否真正了解TransactionScope?
查看>>
JS~模拟表单在新窗口打开,避免广告拦截
查看>>
ANSI最全介绍linux终端字体改变颜色等
查看>>
数据结构5——堆
查看>>
11Linux_sshd_Apache
查看>>
Android hdpi ldpi mdpi xhdpi xxhdpi适配详解
查看>>
Java_4.1 猜数字游戏
查看>>
Openstack的mysql数据多主galera的错误
查看>>
文件关联程序
查看>>