0

我得到了一个FileSystemWatcher,当我在第一个目录中创建一个文件时,它也应该进入第二个目录。所以我希望文件已经存在于第二个目录中时被覆盖。它不会工作。我懂了:

 private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
    {

            listBox1.Items.Add("File created> " + e.FullPath + " -Date:" + DateTime.Now);
            if (File.Exists(target+e.Name))
            {
                File.Delete(target+e.Name);
            }
            File.Copy(e.FullPath, Path.Combine(target, e.Name));
    }

我遇到了另一个问题:它显示文件的创建时间,而不是重命名、删除、更改的时间。这是我的代码:

private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File changed> " + e.FullPath + " -Date:" + DateTime.Now);
        }

    }

    private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
    {

            listBox1.Items.Add("File created> " + e.FullPath + " -Date:" + DateTime.Now);
            if (File.Exists(target+e.Name))
            {
                File.Delete(target+e.Name);
            }
            File.Copy(e.FullPath, Path.Combine(target, e.Name));
    }

    private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File deleted> " + e.FullPath + " -Date:" + DateTime.Now);
            File.Delete(target+e.Name);
        }
    }

    private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File renamed> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }
        private void Start_Click(object sender, EventArgs e)
    {
        fileSystemWatcher1.Path = source;
        if (!pause)
        {
            pause = true;
            Start.Text = "Pause";
            fileSystemWatcher1.EnableRaisingEvents = true;   
        }
        else
        {
            pause = false;
            Start.Text = "Start";
            fileSystemWatcher1.EnableRaisingEvents = false;

        }

    }
4

1 回答 1

0

如果(!暂停){

} 导致你必须删除它的问题。

于 2013-06-20T11:42:25.963 回答