いくつかのファイルを作成/配置するディレクトリがあります。filesystemwatcher が新しいファイルを検出すると、そのファイルを 2 番目のディレクトリにコピーします。しかし、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;
}
}
}
}