C# windows service

领导来了封邮件:C# 创建  windows service ,扫描相应目录,并将扫描到文件使用DOS
命令方式调用第三方Ftp工具上传到Ftp服务器。

短短几句话,看了好几遍。分析之后,由于对这个之前没有接触,一步一步来。这里也写一篇,一来跟新人分享下,二来希望前辈拍砖指点。

1.创建windows Service 服务。

(这里参考http://blog.csdn.net/yysyangyangyangshan/article/details/10515035 前辈的博文,不过前辈的博文有两点没懂,一是安装代码,我不知道写在哪,也不知道利用什么事件触发调用它,所以安装的时候,用命令行手动安装了。二是当服务运行起来的时候,在运行目录下没有看到记录的文件,而是当我停止或者卸载服务的时候,在目录下有这个记录文件,因此也拜读了http://blog.csdn.net/xxj_jing/article/details/7542654
前辈的文章。)

ServiceFtp.cs 的代码:

?





1

2

3

4

5

6

7

8

9

10

11

12

public
ServiceFtp() 

       

          InitializeComponent(); 

       

    

       protected
override void OnStart(string[] args) 

       

       

    

       protected
override void OnStop() 

       

       

要做的方法放在OnStart()里面即可,当启动服务时就可调用。这边记录时间文件。新建一个类:CFileOpreation.cs
用于记录时间操作。

该类的代码完全参考前辈的(嘿嘿):


public class CFileOpreation
{
#region SaveRecord
/// 服务测试:写一个文件到本地
/// <summary>
/// 保存至本地文件
/// </summary>
/// <param name="ETMID"></param>
/// <param name="content"></param>
public static void SaveRecord(string content)
{
if (string.IsNullOrEmpty(content))
{
return;
}
FileStream fileStream = null;
StreamWriter streamWriter = null;
try
{
string path = Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, string.Format("{0:yyyyMMdd}", DateTime.Now));
using (fileStream = new FileStream(path, FileMode.Append, FileAccess.Write))
{
using (streamWriter = new StreamWriter(fileStream))
{
streamWriter.Write(content);
if (streamWriter != null)
{
streamWriter.Close();
}
}
if (fileStream != null)
{
fileStream.Close();
}
}
}
catch { }
}
#endregion
}

然后在ServiceFtp.cs中调用该类中的方法,代码如下:

 1 public ServiceFtp()
2 {
3 InitializeComponent();
4 //InitService();另一个测试使用
5 }
6
7 protected override void OnStart(string[] args)
8 {
9 IntialSaveRecord();
10 string strStart = "服务开启";
11 }
12
13 protected override void OnStop()
14 {
15 if (recordTimer != null)
16 {
17 //释放
18 recordTimer.Dispose();
19 }
20 string strStop = "服务停止";
21 }
22 protected override void OnContinue()
23 {
24 string strCtn = "服务继续运行";
25 base.OnContinue();
26 }
27 protected override void OnPause()
28 {
29 string strPause = "服务暂停";
30 base.OnPause();
31 }
32
33 /// <summary>
34 /// 初始化服务
35 /// </summary>
36 private void InitService()
37 {
38 base.AutoLog = false;
39 base.CanShutdown = true;
40 base.CanStop = true;
41 base.CanPauseAndContinue = true;
42 base.ServiceName = "ServiceFtp"; //这个名字很重要,设置不一致会产生 1083 错误哦!
43 }
44 private void IntialSaveRecord()
45 {
46 TimerCallback timerCallback = new TimerCallback(CallbackTask);
47 AutoResetEvent autoEvent = new AutoResetEvent(false);
48
49 recordTimer = new System.Threading.Timer(timerCallback, autoEvent, 10000, 60000 * 10);
50 }
51 private void CallbackTask(Object stateInfo)
52 {
53 //调用文件操作类的方法
54 CFileOpreation.SaveRecord(string.Format(@"当前记录时间:{0},状况:程序运行正常!", DateTime.Now));
55 }

这边服务写得差不多了,添加下安装程序:

项目中会出现Install的东西:

视图设计器的界面出现:

右键设置一下属性,这里启动方式前辈们一般都是设置为手动,我想试试自动有什么不一样,设为自动:

然后设置一下账户类型:localsystem。

到这就差不多了,生成项目,找到EXE文件,进行添加到服务面板,根据.NET 版本4.0 和2.0 的不同,4.0的同学:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe MyFtpService.exe

2.0的同学:

C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe MyFtpService.exe

上面两行命令在控制台输入即可安装服务,之后在服务面板中就可以看到了,感觉很是亲切:

这是手动控制台安装方式,在网上逛逛,发现了“.bat”文件好像蛮好用的,果断试试:

在EXE文件目录建立两个文件,必须是以“.bat”作为文件格式,叫做安装服务.bat和卸载服务.bat

安装服务.bat代码(我的版本是4.0):

C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe MyFtpService.exe
  pause

卸载服务.bat代码:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe
/u MyFtpService.exe
  pause

pause的作用:保持控制台窗口不关闭

这两个方便,双击下就安装/卸载好了。

运行下,一下是没有记录时间文件,可是我卸载服务后,却发现有,求指点:

上面写入时间已经完成,但是我还加了个扫描指定目录的代码:

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

//root dir

const
string strPath = @"D:/DOS命令扫描目录";

//go through dir

private
void ExploreDir(DirectoryInfo di)

{

    //write dir name

    Console.WriteLine(di.FullName);

    //write file name

    FileInfo[] files = di.GetFiles();

    foreach
(FileInfo item in
files)

    {

        Console.WriteLine(item.FullName);

    }

    // recursive do it

    DirectoryInfo[] dirs = di.GetDirectories();

    foreach
(DirectoryInfo d in
dirs)

    {

        ExploreDir(d);

    }

    Console.ReadLine();   

调用一下该递归方法

?





1

2

3

Program pp = new
Program();

DirectoryInfo dir = new
DirectoryInfo(strPath);

pp.ExploreDir(dir);       

扫描的目录文件中只能识别常见格式的文件,rar txt doc, VS 生成的这么多文件他一个都没扫出来。

接下来就要使用DOS 命令调用第三方Ftp 工具将扫描到的文件发送到Ftp服务器,热烈欢迎前辈们指点。

C# windows service

时间: 2024-08-30 02:18:19

C# windows service的相关文章

Windows service wrapper 初探

Windows 服务包装器(Windows service wrapper),用于把.exe文件注册为windows服务.比如把Nginx.exe注册为windows服务,这样做的好处是,每次启动nginx时不用在命令行中输入命令,而且可以随windows系统启动而启动.不用担心服务器意外重启,服务挂掉. github地址:https://github.com/kohsuke/winsw 下载地址:https://github.com/kohsuke/winsw/releases 目前(2017

C# Windows Service中执行死循环轮询

用C#编写Windows Service时,执行轮询一般有两种方式,一种是用Timer,System.Timers或者是System.Thread下的,这种执行是按时间循环执行,缺点是也许上个执行还没有完成,又开始执行新的. 另一种方式是利用线程,在OnStart里单开一个线程去跑含有死循环结构的函数,这种方式的缺点是,对线程的控制困难,停止服务了,线程还有可能在执行,不过 .Net 4.0+ 给我们提供了 CancellationTokenSource,用来取消正在运行的线程(Task),代码

管理员控制Windows Service

C# 以管理员方式启动Winform,进而使用管理员控制Windows Service 问题起因: 1,) 问题自动分析Windows服务在正常运行时,确实会存在程序及人为原因导致该服务停止.为了在应用程序使用时确保该服务正常运行,于是有了该讨论主题. 2,)一般账户(尽管是管理员组账户)使用c#代码启动服务,依然会抛出异常,因为当前程序启动账户级别并不是管理员级别. 以管理员启动应用程序解决方案及测试: 为了解决程序以管理员组角色启动应用程序,我们需要在应用程序的工程中添加一个“Applica

在Windows Service 2012上安装IIS 8.0 IIS 6

我的目的是在服务器上安装IIS6 ,但是受到这边文章的启发和按照他的步骤,看到了"IIS 6管理兼容性",我的问题就决解了,我这里是因为要安装vss 2005 和u8等比较早期的软件才会遇到这个问题: 下面内容转载自:http://www.zhaomu.com/news/detail-394.html 内容如下: Windows 2012及其自带的IIS 8.0是微软公司新一代的Web服务器软件,和老版本的IIS相比,有很多破天荒的新功能.随着微软宣布不再支持Windows XP操作系

C# 创建Windows Service

当我们需要一个程序长期运行,但是不需要界面显示时可以考虑使用Windows Service来实现.这篇博客将简单介绍一下如何创建一个Windows Service,安装/卸载Windows Service. 新建Windows Service项目: 删除自动生成的Service1.cs文件,新建WindowsService类,继承ServiceBase. class WindowsService : ServiceBase { public WindowsService() { this.Ser

C# 开发Windows Service程序控制功能

在做一些计划任务时候难免用到Windows Service服务程序,而这个是没有操作界面的,每次启动.重启.关闭都需要服务界面找到服务进行操作,对普通的人来说是非常麻烦的,所以有时候就需要通过应用程序来控制Windows 服务,这里把之前写到的一个服务控制类贴出来. C# Windows 服务控制类代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste

WCF注册Windows Service

WCF注册Windows Service 2014-06-14 返回 在前面创建一个简单的WCF程序,我们把WCF的服务寄宿到了Host这个控制台项目中了.下面将介绍如何把WCF的服务寄宿到Windows服务中: 1. 删除原来Host控制台项目,然后在solution上右键,新建一个WindowService项目.如下图: 2.对MyFirstWindowsService项目添加对Contracts项目.Service项目和System.ServiceModel的引用. 3.将MyFristW

C#创建一个Windows Service

Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Windows Service写很深入. 本文介绍了如何用C#创建.安装.启动.监控.卸载简单的Windows Service 的内容步骤和注意事项. 一.创建一个Windows Service 1)创建Windows Service项目 2)对Service重命名 将Service1重命名为你服务名称,这里我

WCF: Hosting WCF in Windows Service

1. Create a windows service project 2. Add Reference to the assembly which contains the contract and its implementation. 3. Remove the Service1.cs, add a new Windows Service class and name it to CalculatorWindowsService 4. Override OnStart and OnStop

【翻译自mos文章】Windows Service Oracleremexecservice 是干什么的

来源于: What Is The Windows Service "Oracleremexecservice" ? (文档 ID 1433144.1) 适用于: Oracle Server - Enterprise Edition - Version 11.2.0.1 and later Oracle Server - Standard Edition - Version 11.2.0.1 and later Microsoft Windows (32-bit) Microsoft W