【问题标题】:FileSystemWatcher: multiple typeFileSystemWatcher:多种类型
【发布时间】:2013-10-25 15:02:08
【问题描述】:

我想发送一个在系统观察程序发现创建的以 ".txt"、".doc" 或 ".docx" 结尾的文件时找到的文件"。我的问题是系统观察程序只发现以“.txt”结尾的文件。

这是我的代码:

private String Attachmenttosend
{
    get { return attachmentfile; }
    set { attachmentfile = value; }
}

private void NewFileSystemWatcher()
{
    String filter = "*.txt,*.doc,*.docx";   
    string[] FileExtension = filter.Split(',');  
    for (int i = 0; i < FileExtension.GetLength(0); i++)
    {
        watcher = new FileSystemWatcher(folder); // on its own Thread
        watcher.Created += new FileSystemEventHandler(NewEMail); 
        attachmenttosend.Add(Attachmenttosend); 
        watcher.Filter = FileExtension[i];
        watcher.EnableRaisingEvents = true;
        watchlist.Add(watcher);
    }
    Send(Attachmenttosend); 
}

private void NewEMail(Object source, FileSystemEventArgs e)
{
    while (Locked(e.FullPath)) // check if the file is used
    {
        Thread.Sleep(10);
    }
    Attachmenttosend = e.FullPath; // store the filename 
}

【问题讨论】:

  • 您想捕捉特定目录中任何文件的每一次修改吗?
  • 如果创建了一个(或更多)文件(以 .txt、.doc 或 .docx 结尾),那么系统观察者是否应该发现该/那些文件并将文件名返回给我.它仅适用于 *.txt。就像在链接stackoverflow.com/questions/6965184/… 中一样,我为每个文件扩展名创建了一个 FileSystemWatcher 对象。
  • 尝试注释掉新邮件中的附件行和while循环,并通过在NewEMail中打印来检查它是否返回事件。
  • 这一行做了什么“attachmenttosend.Add(Attachmenttosend);”

标签: c# filesystemwatcher


【解决方案1】:

我认为这会对您有所帮助,只需动态创建一个控制台应用程序并将其粘贴并尝试一下:

        private static string[] filters = new string[] { ".txt", ".doc", ".docx" };

        static void Main(string[] args)
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = @"C:\...\...\...";//your directory here
            /* Watch for changes in LastAccess and LastWrite times, and
               the renaming of files or directories. */
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
               | NotifyFilters.FileName | NotifyFilters.DirectoryName;

            //dont set this
            //watcher.Filter = "*.txt";

            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnChanged);
            watcher.Deleted += new FileSystemEventHandler(OnChanged);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);

            watcher.EnableRaisingEvents = true;
            Console.ReadKey();
        }

        private static void OnChanged(object source, FileSystemEventArgs e)
        {          
            if(filters.Contains(Path.GetExtension(e.FullPath)))
            {
                Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
                //Attachmenttosend = e.FullPath;
            }
        }

        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            if (filters.Contains(Path.GetExtension(e.FullPath)))
            Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
        }

正如库纳尔指出的那样

attachmenttosend.Add(Attachmenttosend);

我从大写和小写中猜想您正在尝试将其自己的属性添加到属性的支持字段,不要,也...您不要仅添加到字符串 += (concat)。 除非 attachmenttosend 是一个例如字符串列表。

【讨论】:

  • attachmenttosend 是一个 List :它确实添加了系统观察者在使用此扩展名之一“.txt、.doc 或 *.docx”之一创建文件时发现的文件名跨度>
猜你喜欢
  • 1970-01-01
  • 2016-09-20
  • 2016-09-09
  • 2020-08-03
  • 2019-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多