我有一个目录,我在其中制作/放置一些文件。当文件系统观察器看到一个新文件时,它会将该文件复制到第二个目录中。但是当我直接在 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;
}
}
}
}