FileSystemWatcher的双项

FileSystemWatcher的双项

本文介绍了FileSystemWatcher的双项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个小的WinForms应用程序来监控新的PDF文件的某个文件夹,如果在particulair文件夹中创建一个新的PDF文件,将它复制到其他位置。



我遇到的问题是,FileSystemWatcher的创建在我的列表框双/多条目,我怎么能解决这个问题?

 命名空间Scanmonitor 
{
公共部分Form1类:表格
{
FileSystemWatcher的守望者=新FileSystemWatcher的();
的DateTime lastRead = DateTime.MinValue;


公共Form1中()
{
的InitializeComponent();
}

私人无效Form1_Load的(对象发件人,EventArgs五)
{
FileWatch();
}

公共无效FileWatch()
{
watcher.Path = @C:\Scanner
watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite;
watcher.Filter =* .PDF;
watcher.Changed + =新FileSystemEventHandler(调用onChanged);
watcher.EnableRaisingEvents = TRUE;
}

公共无效调用onChanged(对象源,FileSystemEventArgs E)
{
scannerListBox.Items.Add(e.FullPath);
scannerListBox.SelectedIndex = scannerListBox.Items.Count - 1;
FileMove(scannerListBox.SelectedItem.ToString());
}

公共无效FileMove(字符串文件路径)
{

{
System.IO.File.Copy(文件路径,@ \\share\Data\Scans运OCE 600\+ Path.GetFileName(文件路径));
}
赶上(异常前)
{
ToolLabel.Text = ex.Message.ToString();
}
}
}
}



}



得到它的工作。

 公共无效调用onChanged(对象源, FileSystemEventArgs E)
{

{
watcher.EnableRaisingEvents = FALSE;
FileInfo的objFileInfo =新的FileInfo(e.FullPath);
如果(objFileInfo.Exists!)回报;
System.Threading.Thread.Sleep(5000);

FileInfo的fileinformatie =新的FileInfo(e.FullPath);
串strCreateTime = fileinformatie.CreationTime.ToString();
串strCreateDate = fileinformatie.CreationTime.ToString();

strCreateTime = strCreateTime.Remove(strCreateTime.LastIndexOf());
strCreateDate = strCreateDate.Remove(0,strCreateDate.LastIndexOf());

ProcessAllFiles(e.FullPath,strCreateTime,strCreateDate);
}
赶上(异常前)
{
ToolLabel.Text = ex.Message.ToString();
}
终于
{
watcher.EnableRaisingEvents = TRUE;
}
}


解决方案

 公共无效调用onChanged(对象源,FileSystemEventArgs E)
{

{
watcher.EnableRaisingEvents = FALSE;
FileInfo的objFileInfo =新的FileInfo(e.FullPath);
如果(objFileInfo.Exists!)回报;
System.Threading.Thread.Sleep(5000);

FileInfo的fileinformatie =新的FileInfo(e.FullPath);
串strCreateTime = fileinformatie.CreationTime.ToString();
串strCreateDate = fileinformatie.CreationTime.ToString();

//忽略此,只为我的文件信息。
strCreateTime = strCreateTime.Remove(strCreateTime.LastIndexOf());
strCreateDate = strCreateDate.Remove(0,strCreateDate.LastIndexOf());

ProcessAllFiles(e.FullPath,strCreateTime,strCreateDate);
}
赶上(异常前)
{
ToolLabel.Text = ex.Message.ToString();
}
终于
{
watcher.EnableRaisingEvents = TRUE;
}
}


I made a small winforms application to monitor a certain folder for new pdf files, if a new pdf file is created in the particulair folder it will copy it to an other location.

The problem i'm having is that the filesystemwatcher creates double/multiple entries in my listbox, how can i solve this?

namespace Scanmonitor
{
    public partial class Form1 : Form
    {
        FileSystemWatcher watcher = new FileSystemWatcher();
        DateTime lastRead = DateTime.MinValue;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FileWatch();
        }

        public void FileWatch()
        {
            watcher.Path = @"C:\Scanner";
            watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite;
            watcher.Filter = "*.pdf";
            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.EnableRaisingEvents = true;
        }

        public void OnChanged(object source, FileSystemEventArgs e)
        {
            scannerListBox.Items.Add(e.FullPath);
            scannerListBox.SelectedIndex = scannerListBox.Items.Count - 1;
            FileMove(scannerListBox.SelectedItem.ToString());
        }

        public void FileMove(string filePath)
        {
            try
            {
                System.IO.File.Copy(filePath, @"\\share\Data\Scans op OCE 600\" + Path.GetFileName(filePath));
            }
            catch (Exception ex)
            {
                ToolLabel.Text = ex.Message.ToString();
            }
        }
    }
}

}

Got it working.

public void OnChanged(object source, FileSystemEventArgs e)
    {
        try
        {
            watcher.EnableRaisingEvents = false;
            FileInfo objFileInfo = new FileInfo(e.FullPath);
            if (!objFileInfo.Exists) return;
            System.Threading.Thread.Sleep(5000);

            FileInfo fileinformatie = new FileInfo(e.FullPath);
            string strCreateTime = fileinformatie.CreationTime.ToString();
            string strCreateDate = fileinformatie.CreationTime.ToString();

            strCreateTime = strCreateTime.Remove(strCreateTime.LastIndexOf(" "));
            strCreateDate = strCreateDate.Remove(0,strCreateDate.LastIndexOf(" "));

            ProcessAllFiles(e.FullPath, strCreateTime, strCreateDate);
        }
        catch (Exception ex)
        {
            ToolLabel.Text = ex.Message.ToString();
        }
        finally
        {
            watcher.EnableRaisingEvents = true;
        }
    }
解决方案
public void OnChanged(object source, FileSystemEventArgs e)
{
    try
    {
        watcher.EnableRaisingEvents = false;
        FileInfo objFileInfo = new FileInfo(e.FullPath);
        if (!objFileInfo.Exists) return;
        System.Threading.Thread.Sleep(5000);

        FileInfo fileinformatie = new FileInfo(e.FullPath);
        string strCreateTime = fileinformatie.CreationTime.ToString();
        string strCreateDate = fileinformatie.CreationTime.ToString();

        //Ignore this, only for my file information.
        strCreateTime = strCreateTime.Remove(strCreateTime.LastIndexOf(" "));
        strCreateDate = strCreateDate.Remove(0,strCreateDate.LastIndexOf(" "));

        ProcessAllFiles(e.FullPath, strCreateTime, strCreateDate);
    }
    catch (Exception ex)
    {
        ToolLabel.Text = ex.Message.ToString();
    }
    finally
    {
        watcher.EnableRaisingEvents = true;
    }
}

这篇关于FileSystemWatcher的双项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 17:35