zoukankan      html  css  js  c++  java
  • C# FileSystemWatcher

    using System.IO;

    //file system watcher object.
            private FileSystemWatcher watcher;
            
    private delegate void UpdateWatchTextDelegate(string newText);

            
    public void UpdateWatchText(string newText)
            {
                lblWatch.Text 
    = newText;
            }

            
    public Form1()
            {
                InitializeComponent();

                
    this.watcher = new FileSystemWatcher();
                
    this.watcher.Deleted += new FileSystemEventHandler(watcher_Deleted);
                
    this.watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
                
    this.watcher.Changed += new FileSystemEventHandler(watcher_Changed);
                
    this.watcher.Created += new FileSystemEventHandler(watcher_Created);
            }

            
    void watcher_Created(object sender, FileSystemEventArgs e)
            {
                
    //throw new NotImplementedException();
                try
                {
                    StreamWriter sw 
    = new StreamWriter("c:\\Log.txt"true);
                    sw.WriteLine(
    "File: {0} Created", e.FullPath);
                    sw.Close();
                    
    this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Wrote create event to log");
                }
                
    catch (IOException)
                {
                    
    this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Error Writing to log");
                }
            }

            
    void watcher_Changed(object sender, FileSystemEventArgs e)
            {
                
    //throw new NotImplementedException();
                try
                {
                    StreamWriter sw 
    = new StreamWriter("c:\\Log.txt"true);
                    sw.WriteLine(
    "File: {0} {1}", e.FullPath, e.ChangeType.ToString());
                    sw.Close();
                    
    this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Wrote change event to log");
                }
                
    catch (IOException)
                {
                    
    this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Error Writing to log");
                }
            }

            
    void watcher_Renamed(object sender, RenamedEventArgs e)
            {
                
    //throw new NotImplementedException();
                try
                {
                    StreamWriter sw 
    = new StreamWriter("c:\\Log.txt"true);
                    sw.WriteLine(
    "File renamed from {0} to {1}", e.OldName, e.FullPath);
                    sw.Close();
                    
    this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Wrote renamed event to log");
                }
                
    catch(IOException)
                {
                    
    this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Error Writing to log");
                }
            }

            
    void watcher_Deleted(object sender, FileSystemEventArgs e)
            {
                
    //throw new NotImplementedException();
                try
                {
                    StreamWriter sw 
    = new StreamWriter("c:\\Log.txt"true);
                    sw.WriteLine(
    "File: {0} Deleted", e.FullPath);
                    sw.Close();
                    
    this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Wrote delete event to log");
                }
                
    catch (IOException)
                {
                    
    this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Error Writing to log");
                }
            }

            
    private void cmdBrowse_Click(object sender, EventArgs e)
            {
                
    if (FileDialog.ShowDialog() != DialogResult.Cancel)
                {
                    txtLocation.Text 
    = FileDialog.FileName;
                    cmdWatch.Enabled 
    = true;
                }
            }

            
    private void cmdWatch_Click(object sender, EventArgs e)
            {
                watcher.Path 
    = Path.GetDirectoryName(txtLocation.Text);//监控路径(文件夹)
                watcher.Filter = "*.*" ;//如果filter为文件名称则表示监控该文件,如果为*.txt则表示要监控指定目录当中的所有.txt文件
                watcher.NotifyFilter = NotifyFilters.LastWrite |
                    NotifyFilters.FileName 
    |
                    NotifyFilters.Size;
                lblWatch.Text 
    = "Watching " + txtLocation.Text;

                
    //begin watching.
                watcher.EnableRaisingEvents = true;
            }
     

    青苹果Web应用商店 https://webapp.taobao.com/

    PHP/ASP.NET/ASP/UCHOME/DISCUZ! X系列网站开发,详细需求联系QQ:8511978

  • 相关阅读:
    Content delivery network
    散列算法的基础原理 确保资料传递无误
    科学计算 NumPy 与C语言对比 N-dimensional array ndarray 元素元素操作 计算正太分布分位数 ndarray中的所有元素的类型都是相同的,而Python列表中的元素类型是任意的,所以ndarray在存储元素时内存可以连续,而python原生list就只能通过寻址方式找到下一个元素
    t
    百度 url 当在baidu搜索结果展示页,去点击标头时
    指定文件夹 指定文件后缀名 删除整个文件夹 git 冲突解决 create a new repository on the command line push an existing repository from the command line rebase
    修改MojoWeixin 只保留用户name 取消群昵称
    AnyEvent::HTTP 介绍
    AnyEvent::HTTP 介绍
    异步和同步http请求超时机制
  • 原文地址:https://www.cnblogs.com/Dicky/p/CSharp_FileSystemWatcher.html
Copyright © 2011-2022 走看看