0

我想发送一个在系统观察程序发现创建的以" .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 
}
4

1 回答 1

0

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

        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);
        }

正如库纳尔指出的那样

附件发送。添加(附件发送);

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

于 2013-10-24T13:58:07.313 回答