WindowsService
创建
配置
添加服务
工程右键 => 添加 => 新建项
修改服务入口
添加安装程序
配置服务参数
编写
安装
安装和卸载主要使用的是.NET提供的 InstallUtil.exe
这个文件 ,文件位于C盘对应的目录下 C:\Windows\Microsoft.NET\Framework64\v4.0.30319
,拷贝至和exe同一个目录bin下。
拷贝完成后再执行下方操作,需要管理员权限。(注意:安装后不要再用VisualStudio修改服务,若需要修改先卸载服务,否则服务卸载会提示卸载异常,未完全卸载!)
1
| installutil TestService.exe
|
启动
调试
停止
卸载
1
| installutil /u TestService.exe
|
FileSystemWatcher 类
定义
侦听文件系统更改通知,并在目录或目录中的文件发生更改时引发事件。
属性
方法
事件
示例(WindowsService+FileSystemWatcher)
TestService.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.ServiceProcess;
namespace TextWindowsService { partial class TestService : ServiceBase { FileSystemWatcher watcher = null; static Log log = null;
public TestService() { InitializeComponent(); }
protected override void OnStart(string[] args) { Init(); }
protected override void OnStop() { log.Dispose(); }
void Init() { log = new Log(@"D:\FileSystemWatcher"); watcher = new FileSystemWatcher(); watcher.Path = @"D:\FileSystemWatcher"; watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
watcher.Changed += OnChanged; watcher.Created += OnCreated; watcher.Deleted += OnDeleted; watcher.Renamed += OnRenamed; watcher.Error += OnError;
watcher.Filter = "*.txt"; watcher.IncludeSubdirectories = true; watcher.EnableRaisingEvents = true;
log.WriteInfo("Press enter to exit."); Console.WriteLine("Press enter to exit."); Console.ReadLine(); }
private static void OnChanged(object sender, FileSystemEventArgs e) { if (e.ChangeType != WatcherChangeTypes.Changed) { return; } log.WriteInfo($"Changed: {e.FullPath}"); }
private static void OnCreated(object sender, FileSystemEventArgs e) { string value = $"Created: {e.FullPath}"; log.WriteInfo(value); }
private static void OnDeleted(object sender, FileSystemEventArgs e) => log.WriteInfo($"Deleted: {e.FullPath}");
private static void OnRenamed(object sender, RenamedEventArgs e) { log.WriteInfo($"Renamed:"); log.WriteInfo($" Old: {e.OldFullPath}"); log.WriteInfo($" New: {e.FullPath}"); }
private static void OnError(object sender, ErrorEventArgs e) => PrintException(e.GetException());
private static void PrintException(Exception ex) { if (ex != null) { log.WriteInfo($"Message: {ex.Message}"); log.WriteInfo("Stacktrace:"); log.WriteInfo(ex.StackTrace); Console.WriteLine(); PrintException(ex.InnerException); } } } }
|
Log.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| using System; using System.IO;
namespace TextWindowsService { class Log { string LogFilePath = null; StreamWriter sw = null; string LogFileName = null;
public Log(string logFilePath) { this.LogFilePath = logFilePath; InitLogFile(); }
public void WriteInfo(string msg) { sw.WriteLine(msg); }
public void Dispose() { sw.WriteLine($"========================= End_{DateTime.Now:yyyy-MM-dd hh:mm:ss} ========================="); sw.WriteLine(""); sw.WriteLine(""); sw.Close(); }
public void OpenLog() { System.Diagnostics.Process.Start("Explorer", "/start," + Path.Combine(LogFilePath, LogFileName)); }
void InitLogFile() { if (!Directory.Exists(LogFilePath)) Directory.CreateDirectory(LogFilePath); LogFileName = $"{DateTime.Now:yyyy-MM-dd}.log"; this.sw = new StreamWriter(Path.Combine(LogFilePath, LogFileName), true); sw.WriteLine($"========================= Start_{DateTime.Now:yyyy-MM-dd hh:mm:ss} ========================="); } } }
|
注意:本示例只有在关闭服务时才会调用写入流的Close()
方法,只有调用Close()
方法之后才能看见日志内容。
。