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

  • 相关阅读:
    (转)深入浅出JWT(JSON Web token)
    Node.js Koa2开发微信小程序服务端
    天翼宽带家庭网关用户:useradmin,nE7jA%5m 这个是中国电信的超级密码
    微信小程序picker重写,精确到时分秒
    Vue props中Object和Array设置默认值
    GreenDao学习
    Android注解支持(Support Annotations) (转)
    异常:Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details.
    精通 Android Data Binding
    Android BroadcastReceiver介绍 (转)
  • 原文地址:https://www.cnblogs.com/Dicky/p/CSharp_FileSystemWatcher.html
Copyright © 2011-2022 走看看