zoukankan      html  css  js  c++  java
  • FileSystemWatch 对文件的监测 修改 创建 删除 并写入日志

    我做的这个主要是用来对文件的监测 修改 创建 删除 并写入日志

    此外我做的WinForm程序

    首先导入命名空间

    1 using System.IO;

    申明全局变量

    1 private FileSystemWatcher watch = new FileSystemWatcher();
    2 private FileStream fs = null;

    先写一个写日志的方法,我这里写的路径都是固定的,你们可以给这个路径配置在App.config中,用户可以选择路径,选择完以后

    修改config文件,下次用的时候可以默认选择上次的路径,这里我就不做了

    1 public void write(string message)
    2 {
    3 string path = "C:\\Log\\";
    4 if (!Directory.Exists(path))
    5 {
    6 Directory.CreateDirectory(path);
    7 }
    8 string fileName = path + "Log.txt";
    9
    10 if (!File.Exists(fileName))
    11 {
    12 FileStream file = new FileStream(fileName, FileMode.Create);
    13 file.Dispose();
    14 }
    15 fs = new FileStream(fileName, FileMode.Append, FileAccess.Write);
    16 StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
    17 sw.WriteLine(message);
    18 sw.WriteLine("-----------------------");
    19 sw.Dispose();
    20 fs.Dispose();
    21 }

    写监测的方法,我这里只是记录了一部分信息

    1 public void ReNameWatch(object sender, FileSystemEventArgs e)
    2 {
    3 write(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " ReName File : " + e.Name.ToString());
    4 }
    5
    6 public void DeleteWatch(object sender, FileSystemEventArgs e)
    7 {
    8 write(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " Delete File : " + e.Name.ToString());
    9 }
    10
    11 public void CreateWatch(object sender, FileSystemEventArgs e)
    12 {
    13 write(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " Create File : " + e.Name.ToString());
    14 }
    15
    16 public void ChangeWatch(object sender, FileSystemEventArgs e)
    17 {
    18 write(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " Change File : " + e.Name.ToString());
    19 }

    写按钮点击事件

    1 public void Watch(object sender, EventArgs e)
    2 {
    3 if (this.button1.Text == "Start")
    4 {
    5 string path = "C:\\";
    6 watch.Path = path;
    7 watch.Filter = "*.txt";
    8 watch.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite | NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.LastAccess | NotifyFilters.Security;
    9
    10 watch.Renamed += new RenamedEventHandler(ReNameWatch);
    11 watch.Deleted += new FileSystemEventHandler(DeleteWatch);
    12 watch.Created += new FileSystemEventHandler(CreateWatch);
    13 watch.Changed += new FileSystemEventHandler(ChangeWatch);
    14
    15 watch.EnableRaisingEvents = true;
    16 this.button1.Text = "Stop";
    17 }
    18 else if (this.button1.Text == "Stop")
    19 {
    20 watch.EnableRaisingEvents = false;
    21 this.button1.Text = "Start";
    22 }
    23 }

    写加载事件

    1 public Two()
    2 {
    3 InitializeComponent();
    4 this.KeyDown += new KeyEventHandler(Two_KeyDown);
    5 this.button1.Click += new EventHandler(Watch);
    6 }

    生成日志文件Log.txt截图

  • 相关阅读:
    Oracle数据库的左连接和右连接(+)
    Web文件上传模块 Plupload
    增加反向链接的35个技巧
    google map api 与jquery结合使用(1)控件,监听器[转帖]
    教你在windows 7/xp 下安装使用mencoder
    Oracle 全文索引
    提高关键词排名的28个SEO技巧
    二叉树遍历及C语言实现
    小额担保业务管理系统详细设计介绍
    C#与数据结构二叉树的遍历
  • 原文地址:https://www.cnblogs.com/wangyuanxun/p/2086207.html
Copyright © 2011-2022 走看看