准备工作:
.电脑->管理->本地用户和组->组->Administrator双击->隶属->添加Network service->确定
.启动windows服务Windows Installer
.创建winform项目 WindowsFormsApplication1
.添加windows服务 service1
.添加代码
protected override void OnStart(string[] args)
{
if (args != null && args.Length > )
{
if (args[] == "")
{
string path = $@"d:\kxbbbb{DateTime.Now.ToLongDateString()}.txt";
File.Create($"{path}");
}
else if (args[] == "")
{
string path = $@"d:\kxqqq{DateTime.Now.ToLongDateString()}.txt";
File.Create($"{path}"); }
} // TODO: 在此处添加代码以启动服务。
}
.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);
} .service1.cs右键查看设计器,再右键添加安装程序,默认添加两个
serviceInstaller1和serviceProcessInstaller1
.serviceInstaller1右键属性修改Description 为这是一个测试服务
.serviceProcessInstaller1右键属性 Account修改为NetworkService
.管理员打开cmd
cd C:\Windows\Microsoft.NET\Framework(64)\v4.0.30319 把InstallUtil.exe复制到要发布的EXE的debug目录里面,命令切换等到该DEBUG目录
.安装服务
InstallUtil.exe WindowsFormsApplication1.exe
.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 = { "" };
service.Start(args);
service.WaitForStatus(ServiceControllerStatus.Running);
}

webform调用windows服务的更多相关文章

  1. Windows服务调用Quartz.net 实现消息调度

    Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...

  2. .net MVC, webAPI,webForm集成steeltoe+springcloud实现调用服务中心服务的总结

    开始之前,如果没接触过Autofac的,可以移步到Autofac官方示例学习一下怎么使用:https://github.com/autofac/Examples .net 下集成steeltoe进行微 ...

  3. 使用C#创建及调用WCF完整实例 (Windows服务宿主)

    关于WCF的概念.原理.优缺点等,在这里就不多说了,网上很多,可以自行搜索,比我解释的要专业的多. 这里直接说使用Windows 服务(Windows Service)作为宿主如何实现,其它方式不在此 ...

  4. windows服务是如何被调用的?

    1.服务介绍 操作系统在启动的时候,会启动一些不需要用户交互的进程.这些进程被称为服务.当操作系统启动后它就自动被运行. 2.组成 服务程序.服务控制程序(SCP,service control pr ...

  5. C#使用windows服务定时调用api接口

    使用VS创建windows服务项目: 创建好项目  会出现一个设计界面 右键弹出对话框 选择添加安装程序 名字什么的自己可以改: 项目目录: 打开项目中的ProjectInstaller.Design ...

  6. Windows服务的新建,安装,卸载,调试以及调用!

    一.前言: 写这篇博文之前,我正顶着压力在尝试着调试我一无所知的Windows自建服务.历经千辛万苦,找了无数零散文档拼凑关于VisualStudio2015中怎样创建和调试服务程序!最后终于调试成功 ...

  7. Python搭建调用本地dll的Windows服务(浏览器可以访问,附测试dll64位和32位文件)

    一.前言说明 博客声明:此文链接地址https://www.cnblogs.com/Vrapile/p/14113683.html,请尊重原创,未经允许禁止转载!!! 1. 功能简述 (1)本文提供生 ...

  8. 以 Console 方式运行、调试、编译 .Net 编写的 Windows 服务

    经常看到一些人在调试 Windows 服务时,很执著的在附加进程后调试!其实 .Net 编写的 Windows 应用程序,包括 Windows 服务都可以编译成 Console 程序!甚至于 ASP. ...

  9. 基于SignalR实现B/S系统对windows服务运行状态的监测

    通常来讲一个BS项目肯定不止单独的一个BS应用,可能涉及到很多后台服务来支持BS的运行,特别是针对耗时较长的某些任务来说,Windows服务肯定是必不可少的,我们还需要利用B/S与windows服务进 ...

随机推荐

  1. Request.getInputStrema只能读取一次的分析过程

    1. 我们先来看一下继承关系HttpServletRequest 接口继承ServletRequest接口 public abstract interface  ServletRequest{ pub ...

  2. (简单)冒泡和直接选择排序同时调用swap算法

    void swap(int &a , int &b) { int temp; temp = a; a=b; b=temp; } void bubble(int a[],int n) { ...

  3. memsql filesystem pipeline 试用

    一些功能类似drill ,比如s3,file ... 创建file pipeline 准备file mkdir -p /opt/db/ touch books.txt 内容如下: The Catche ...

  4. 转 HTTP/2: The Long-Awaited Sequel

    HTTP/2: The Long-Awaited Sequel Thursday, October 9, 2014 2:01 AM 6 Ready to speed things up? Here a ...

  5. Fortify SCA 分析代码漏洞全解

    上次介绍了用FindBugs辅助分析代码漏洞.这次换了一个工具:Fortify SCA Demo 4.0.0.Fortify是一个在安全方面挺出名的公司,这里就不多说了.先介绍一下主角:Fortify ...

  6. 关于location.href赋值的php用法

    <?php echo $_GET['action']; ?> <!doctype html> <html lang="zh"> <head ...

  7. GNU Radio: Overview of the GNU Radio Scheduler

    Scetion 1: The Flowgraph The flowgraph moves data from sources into sinks. 一个流图由多个模块组成,其中一般包括信源(Sour ...

  8. 基数排序算法-python实现

    #-*- coding: UTF-8 -*- import numpy as np def RadixSort(a): i = 0 #初始为个位排序 n = 1 #最小的位数置为1(包含0) max ...

  9. VS2015 Git 源代码管理工具使用记录

    1. 首先到源代码托管平台申请个账户:https://git.oschina.net/ 2.创建流程图: 2.1 开始创建项目: 2.2 3. 4.

  10. Bootstrap-Plugin:按钮(Button)插件

    ylbtech-Bootstrap-Plugin:按钮(Button)插件 1.返回顶部 1. Bootstrap 按钮(Button)插件 按钮(Button)在 Bootstrap 按钮 一章中介 ...