これは私の最初のC#の投稿です。バインディングイベント別のクラスでは
イベントバインディングに関する質問があります。
FileWatcherEvents
という別のクラスで定義されている関数にバインドしたいのは、FileWatcher
です。
イベントをProgram
クラスで宣言したくない場合、どうすればいいですか?
ご覧のとおり、私はCreated
とDeleted
のイベントをバインドしようとしています。
問題は、フォルダ内のファイルを削除または作成するときにこれらのイベントが呼び出されないことです。しかし、私がProgram
クラスのイベントハンドラを宣言すると、動作します。
ご意見や洞察力があれば幸いです。
プログラム
using System.IO;
class Program : ServiceBase
{
private FileSystemWatcher _watcher;
public Program()
{
FileWatcherEvents fwe = new FileWatcherEvents();
this._watcher = new FileSystemWatcher();
((System.ComponentModel.ISupportInitialize)(this._watcher)).BeginInit();
//
// _watcher
//
this._watcher.EnableRaisingEvents = true;
this._watcher.Filter = "*.txt";
this._watcher.NotifyFilter =
((NotifyFilters)(((((NotifyFilters.FileName
| NotifyFilters.DirectoryName)
| NotifyFilters.LastWrite)
| NotifyFilters.LastAccess)
| NotifyFilters.CreationTime)));
this._watcher.Path = "T:\\out\\";
this._watcher.Deleted += new FileSystemEventHandler(fwe.ShipmentFileCreated);
this._watcher.Created += new FileSystemEventHandler(fwe.FileDeleted);
((System.ComponentModel.ISupportInitialize)(this._watcher)).EndInit();
}
static void Main(string[] args)
{
Program prg = new Program();
Console.Write(FileManager.getNewestFile("T:\\out\\"));
while (Console.Read() != 'q') ;
}
}
FileWatcherEvents
class FileWatcherEvents
{
public void ShipmentFileCreated(object sender, FileSystemEventArgs e)
{
Console.WriteLine("CREATED: " + sender.ToString() + e.ToString());
}
public void FileDeleted(object sender, FileSystemEventArgs e)
{
Console.WriteLine("DELETED: " + sender.ToString() + e.ToString());
}
}
こんにちは、面白い考え、私はそれを試してみましょう、 – Triztian
いいえ、それは動作しませんでした、それは 'プログラム'のメンバーと宣言し、まだコンソールにログなし – Triztian
イベントが起動されても確実ですか?プログラムクラスのイベントハンドラで動作するようにしましたか? – BlueMonkMN