首页 >> 甄选问答 >

filewatchersystem 移动

2022-08-26 05:12:16

问题描述:

filewatchersystem 移动,有没有人能看懂这题?求帮忙!

最佳答案

推荐答案

2022-08-26 05:12:16

大家好,小金来为大家解答以上的问题。filewatchersystem 移动这个很多人还不知道,现在让我们一起来看看吧!

1、FileWatcher能实现对某一目录的文件(新建,改名,内容修改,删除)的实时监视using System;using System.IO;using System.Windows.Forms;namespace Fw{    public partial class frm1 : Form    {        private FileSystemWatcher watcher;        private delegate void UpdateWatchTextDelegate(string newText);        public frm1()        {            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);        }        public void UpdateWatchText(string newText)        {            lblWatch.Text = newText;        }        public void WriteLog(string LogContent)         {            using (StreamWriter sw = new StreamWriter("c:\Log.txt", true))            {                sw.WriteLine(LogContent);                sw.Close();            }                     }        void watcher_Created(object sender, FileSystemEventArgs e)        {                       try            {                WriteLog(String.Format("File: {0} Created", e.FullPath));                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "文件" + e.FullPath + "被创建");            }            catch (IOException)            {                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "创建日志写入失败!");            }        }        void watcher_Changed(object sender, FileSystemEventArgs e)        {                        try            {                              WriteLog(String.Format("File: {0} {1}", e.FullPath, e.ChangeType.ToString()));                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "文件" + e.FullPath + "被修改");            }            catch (IOException)            {                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "修改日志写入失败!");            }        }        void watcher_Renamed(object sender, RenamedEventArgs e)        {                        try            {                               WriteLog(String.Format("File renamed from {0} to {1}", e.OldName, e.FullPath));                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "文件" + e.OldName + "被重命名为" + e.FullPath);            }            catch (IOException)            {                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "重命名日志写入失败!");            }        }        void watcher_Deleted(object sender, FileSystemEventArgs e)        {                        try            {                                WriteLog(String.Format("File: {0} Deleted", e.FullPath));                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "文件" + e.FullPath + "被删除");            }            catch (IOException)            {                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "删除日志写入失败!");            }        }        private void cmdBrowse_Click(object sender, EventArgs e)        {            if (this.folderBrowserDialog1.ShowDialog() != DialogResult.Cancel)            {                txtLocation.Text = this.folderBrowserDialog1.SelectedPath;                cmdWatch.Enabled = true;            }        }        private void cmdWatch_Click(object sender, EventArgs e)        {            if (txtLocation.Text.Length <= 0)             {                MessageBox.Show("请先选择要监视的文件夹!");                cmdBrowse.Focus();                return;            }            watcher.Path = txtLocation.Text;//监控路径(文件夹)            watcher.Filter = "*.*";//如果filter为文件名称则表示监控该文件,如果为*.txt则表示要监控指定目录当中的所有.txt文件            watcher.NotifyFilter = NotifyFilters.LastWrite |                NotifyFilters.FileName |                NotifyFilters.Size;            lblWatch.Text = watcher.Path + " 监视中";            //begin watching.            watcher.EnableRaisingEvents = true;        }        private void btnStop_Click(object sender, EventArgs e)        {            watcher.EnableRaisingEvents = false;            lblWatch.Text = watcher.Path + " 监视已经停止!";        }    }}注:如果目录下还有子目录,FileWatcher默认情况下并不能监视到子目录下的文件。

2、可以通过设置watcher.IncludeSubdirectories = true; 解决这个问题File Watcher翻译File Watcher 文件监视器;。

本文到此分享完毕,希望对大家有所帮助。

  免责声明:本答案或内容为用户上传,不代表本网观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。 如遇侵权请及时联系本站删除。

 
分享:
最新文章