1

我有一个目录,我在其中制作/放置一些文件。当文件系统观察器看到一个新文件时,它会将该文件复制到第二个目录中。但是当我直接在 dir1 中创建一个文件时,它会关闭说它被另一个程序使用。

我该如何摆脱这个问题?

这是我的代码:

using System;
using System.IO;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace ChaloSync
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private bool pause = false;
    String source = ConfigurationManager.AppSettings[@"Directory1"];
    String target = ConfigurationManager.AppSettings[@"Directory2"];

    private void start()
    {
        pause = true;
        Start.Text = "Pause";
        fileSystemWatcher1.EnableRaisingEvents = true;
    }



    static void config()
    {
        foreach (string key in ConfigurationManager.AppSettings)
        {
            string value = ConfigurationManager.AppSettings[key];
            MessageBox.Show(value);
        }
    }

    private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
    {
            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);
            File.Copy(e.FullPath , Path.Combine(target,e.Name));
    }

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

    private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
    {
            listBox1.Items.Add("File renamed> " + e.FullPath + " -Date:" + DateTime.Now);
    }

    private void Start_Click(object sender, EventArgs e)
    {
        fileSystemWatcher1.Path = source;
        if (!pause)
        {
            start();
        }
        else
        {
            pause = false;
            Start.Text = "Start";
            fileSystemWatcher1.EnableRaisingEvents = false;   

        }

        }
    }
}
4

3 回答 3

1

如果您在读取或写入文件时遇到锁定问题,请检查FileShare选项。

请检查此链接

于 2013-06-20T07:50:32.263 回答
1

不是同一个问题,而是由同一个问题引起的。该解决方案可能对您有好处。

这种方法的一个典型问题是在触发事件时文件仍在被复制。显然,您会得到一个异常,因为文件在复制过程中被锁定。大文件尤其容易出现异常。

As a workaround you could first copy the file and then rename it and listen to the renaming event.

Or another option would be to have a while loop checking whether the file can be opened with write access. If it can you will know that copying has been completed. C# code could look like this (in a production system you might want to have a maximum number of retries or timeout instead of a while(true)):

(Copied from here: File access error with FileSystemWatcher when multiple files are added to a directory)

于 2013-06-20T07:50:39.623 回答
1

Renaming and Creating are being done at the same time. So you can't copy it because it's already being used to rename it at that moment..

于 2013-06-20T07:53:59.637 回答