Visual Studio.net 2010 Windows Service 开发,安装与调试
本示例完成一个每隔一分钟向C:\log.txt文件写入一条记录为例,讲述一个Windows Service 程序的开发,安装与调试
原程序,加文档示例下载
/Files/zycblog/SourceCode.rar
目录索引
1 开发工具
2 开发过程
3 安装
4 开发调试
5 注意事项
6 参考资料
1. 开发工具
1.1 Visual Stuo.net 2010
1.2 Windows 7
2. 开发过程
2.2 打开Visual Studio.net 2010 ,在C# 下边新建Windows Service 程序,命名为FileOperation,保存在D:\study\目录下边,如下图所示
2.3 点击OK以后,展示出新建的项目如下图所示
2.4 其中Program.cs 文件里的程序如下
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace FileOperation
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
}
整个Service程序将从这里开始
默认情况下,VS会为你新建一个名为Service1的服务程序,右击Service1.cs然后选择View Code,打开服务程序的代码窗,程序如下
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace FileOperation
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
}
}
其中OnStart(),OnStop() 根据你的需要写入你在Service程序开始和停止的时候要干什么
2.5 既是要定时往C盘根目录去新建一个文件夹,那就需要一个Timer,并且告知他一分钟执行一次,加上Timer以后代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
namespace FileOperation
{
public partial class Service1 : ServiceBase
{
private readonly Timer _MyTimer;
public Service1()
{
InitializeComponent();
_MyTimer = new Timer(1 * 60 * 1000); //单位为分钟
_MyTimer.Elapsed += _MyTimerElapsed;
}
protected override void OnStart(string[] args)
{
_MyTimer.Start();
}
protected override void OnStop()
{
_MyTimer.Stop();
}
internal void _MyTimerElapsed(object sender, ElapsedEventArgs e)
{
try
{
//添加往C盘新建文本文件的程序
}
catch (Exception ex)
{ }
}
}
}
注意引用 : using System.Timers;
只要在internal void _MyTimerElapsed(object sender, ElapsedEventArgs e) 方法中添加你的程序即可
2.6 既是要文件操作,首先需要添加相关命名空间的引用,以方便你的代码编写
完了以后你的程序如下
{
try
{
//添加往C盘新建一个文本文件的程序
try
{
string logFileName = @"C:\\log.txt"; // 文件路径
FileInfo fileinfo = new FileInfo(logFileName);
using (FileStream fs = fileinfo.OpenWrite())
{
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("=====================================");
sw.Write("添加日期为:" + DateTime.Now.ToString() + "\r\n");
sw.WriteLine("=====================================");
sw.Flush();
sw.Close();
}
}
catch (Exception ex)
{
// ex.ToString();
}
}
catch (Exception ex)
{ }
}
2.7 接下来就程序的安装与调试,看以上新建目录程序是否正常
3. 安装
1.1 右击Service1 选择 View Desinger 打开设计视图,右击设计视图的空白处选择add Installer ,会为你新建ProjectInstaller.cs安装项目文件,打开刚刚新建的ProjectInstaller.cs的设计视图,单击serviceInstaller1,在属性窗口,将ServiceNmae 设置为FileOperation ,StartType 属性设置为Automatic表示系统启动后自动运行服务程序如下图所示,将serviceProcessInstaller1 的Account属性设置为LocalService
3.1 通过以上步骤以后,一个服务程序开发基本完成,你只需要把它安装即可,选择解决方案,生成解决方案,再打开你的方案位置文件夹下,看看你的Bin目录,出现如下文件
FileOperation.exe
FileOperation.pdb
FileOperation.vshost.exe
FileOperation.vshost.exe.manifest
接下来要做的就是要把FileOperation服务程序安装到你的系统当中去,依如下步骤打开Visual Studio.net 2010的命令行程序
如下图所示
3.2 将命令行程序转到你项目bin文件夹下如下图所示
3.3 安装和缷载Windows Service的命令分别是
installutil 服务程序.exe, installutil /u服务程序.exe
这里我先安装项目,输入如下命令
Installutil FileOperation.exe
将出现如下图所示内容
最后内容将看到
The Commit phase completed successfully
the transacted install has completed
表示服务程序已经安装完成
通过以下步骤打开服务窗口,你将看到名为FileOperation服务程序
开始->控制面版->系统与安全->管理工具->服务
你需要手动启动服务,一段时间以后你将看到log.txt 文件里写的信息
4. 开发调试
1.1 Windows service 程序无法像其它程序那样按F5去设置断点来调试,需要采用附加进程的方式,依如下操作便加像平常一样去调试程序了
Debug->Attach to process,选中刚刚开发好的FileOperation进程后,点击attach,便可以设置断点了,由于之前的程序是一分钟执行一次写文件操作,你每隔一分钟将会收到一次调试,需要注意的是,你调试的程序一定要和你安装的服务程序是完全一样的。
5. 注意事项
略
6. 参考文资料
http://msdn.microsoft.com/zh-cn/library/9k985bc9(v=VS.80).aspx
转载于:https://www.cnblogs.com/zycblog/archive/2010/07/02/1769676.html
Visual Studio.net 2010 Windows Service 开发,安装与调试相关推荐
- 【Visual Studio】Visual Studio 2019 创建 Windows 控制台程序 ( 安装 ‘使用 C++ 的桌面开发‘ 组件 | 创建并运行 Windows 控制台程序 )
文章目录 一.安装 C++ 桌面开发组件 二.创建并运行 Windows 控制台程序 一.安装 C++ 桌面开发组件 打开 Visual Studio Installer , 点击 " 修改 ...
- Visual Studio 2008/2010中Xaml开发格式设置技巧
在Silverlight和WPF项目开发中,经常要和Xaml语言打交道,大家都知道,一个清晰明了的开发语言布局可以让开发人员方便,快捷的阅读和理解代码,这里我介绍几个Xaml开发布局设计技巧. 这里我 ...
- 【OpenGL】一、Visual Studio 2019 创建 Windows 桌面程序 ( Visual Studio Installer 安装 C++ 桌面开发库 | 创建桌面程序 )
文章目录 一.Visual Studio Installer 安装 C++ 桌面开发库 二.Visual Studio 2019 创建 Windows 桌面程序 之前曾使用 Visual Studio ...
- [New Portal]Windows Azure Cloud Service (33) 使用Visual Studio 2012 部署Cloud Service
<Windows Azure Platform 系列文章目录> 笔者已经将工作用的笔记本电脑升级到Windows 8很长一段时间了,开发工具也从Visual Studio 2010升级到了 ...
- visual studio 2017 中默认无法开发 Android 8.0 及以上系统的解决方案
一般默认比较旧有两个原因,系统版本过旧,Visual Studio 版本过旧. 第一步,将windows 更新到最新版,必须是windows 10 并且更新到最新. 第二步,将visual studi ...
- 在Visual Studio 2012中使用VMSDK开发领域特定语言(一)
前言 本专题主要介绍在Visual Studio 2012中使用Visualization & Modeling SDK进行领域特定语言(DSL)的开发,包括两个部分的内容.在第一部分中,将对 ...
- 将Visual Studio Code和Windows Subsystem for Linux一起使用
目录 要求 启用Windows Subsystem for Linux 安装Linux(Ubuntu) 将VS Code与WSL一起使用 安装MobaXterm 配置X客户端 安装VS Code 结论 ...
- Microsoft Visual Studio C++2022 Windows 11 SDK环境
Microsoft Visual Studio C++2022 Windows 11 SDK环境 1.安装 2.环境变量 本文为作者(难拳)原创,转载请注明出处. 1.安装 Visual Studio ...
- Windows10下利用Visual Studio Code搭建C语言开发环境
Windows10下利用Visual Studio Code搭建C语言开发环境 1. 前言 2. 下载安装VSCode以及MinGW 2.1 下载安装VSCode 2.2 下载安装MinGW-w64 ...
最新文章
- mysql消重统计_mysql sql查询如何实现发私信用户和其他用户的列表?要求消重所有重复的用户结果 - 贪吃蛇学院-专业IT技术平台...
- JAVASCRIPT实现绚丽TAB选项卡
- 如何一次性复制带有markdown/mathjax/latex的博客内容
- 海量url mysql_海量数据mysql优化步骤
- python登录网页_Python如何爬取需要登录的页面
- linux软件卸载不了,linux软件卸载
- app.vue里使用data_在电脑使用讯飞有声,通过python自动化朗读
- 为何要进行软件维护?维护的种类及目标?
- Lync Server 2013 标准版部署(十)边缘服务器部署[三]
- [Java] 蓝桥杯ALGO-146 算法训练 4-2找公倍数
- LeetCode 94. Binary Tree Inorder Traversal
- 由*.flo(光流)文件生成png图片
- mysql基础之数据库备份和恢复的基础知识
- express 设置handlebars模板引擎
- 温习下 function pointer.
- 深入浅出通信原理笔记(1)
- VMware 未能初始化监视器
- win10家庭版设置护眼色
- 干货|关于云计算认证升级内容
- 工程师思维(简化版)——聪明人的10个工程思维
热门文章
- oracle软件静默安装程序,【oracle】静默安装 oracle 11gr2
- 升级bios_华硕B350PLUS升级BIOS更换AMD 3900X步骤
- 计算机二级web程序设计习题,2015年计算机二级《Web程序设计》习题及答案(5)
- 一维数组求最大值,和三元运算符运算源码
- linux 中阻塞与非阻塞 同步与异步
- C++(8)--数组及常用算法
- 推荐算法--推荐系统冷启动问题(03)
- 《UNIX环境高级编程 3rd》笔记(1 / 21):UNIX基础知识
- LeetCode - Easy - 169. Majority Element
- 演示IPFS的一个完整的流程以及针对部分概念的详解