Hello All,
//请求我有一个简单的问题。我需要,当Filesystemwatcher看到File -after fsw启动关闭应用程序的Timer时(应用程序将在2秒后关闭,何时创建文件)//
感谢用户“永不退出”*
最终我有这个“简单”代码:-)
public partial class Form1 : Form
{
System.Timers.Timer casovac = new System.Timers.Timer();
int totalSeconds = 0;
public Form1()
{
InitializeComponent();
casovac.Interval = 1000;
casovac.Elapsed += new System.Timers.ElapsedEventHandler(cas_elapsed);
}
void cas_elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
totalSeconds++;
if (totalSeconds == 3)
{
casovac.Stop();
Application.Exit();
}
}
private void Form1_Load(object sender, EventArgs e)
{
FileSystemWatcher fsw = new FileSystemWatcher();
fsw.Path = Application.StartupPath + "\\OUT\\";
fsw.Filter = "file.exe";
fsw.IncludeSubdirectories = true;
fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
fsw.Changed += new FileSystemEventHandler(fsw_changed);
fsw.Created += new FileSystemEventHandler(fsw_changed);
fsw.EnableRaisingEvents = true;
}
private void fsw_changed(object source, FileSystemEventArgs e)
{
casovac.Start();
}
}
}
但是谢谢大家; - )
答案 0 :(得分:2)
我认为你想创建一个监视指定路径的FileSystemWatcher
,并在找到“ file.exe ”时为你提供一个事件。一旦你的程序发现这个文件计时器开始,几次(2秒)后你的应用程序会自动关闭。右???
我已经制作了满足您要求的演示。
public partial class Form1 : Form
{
System.Timers.Timer tim = new System.Timers.Timer();
int totalSeconds = 0;
public Form1()
{
InitializeComponent();
tim.Interval = 1000; // 1 sec
tim.Elapsed += new System.Timers.ElapsedEventHandler(tim_Elapsed);
}
void tim_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// .....
totalSeconds++;
if (totalSeconds == 2) // 2 sec of wait
{
tim.Stop();
Application.Exit();
}
}
private void Form1_Load(object sender, EventArgs e)
{
FileSystemWatcher fsw = new FileSystemWatcher("D:\\");
fsw.IncludeSubdirectories = true;
fsw.Filter = "file.exe";
fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
fsw.Changed += new FileSystemEventHandler(OnChanged);
fsw.Created += new FileSystemEventHandler(OnCreated);
fsw.Deleted += new FileSystemEventHandler(OnDeleted);
fsw.Renamed += new RenamedEventHandler(OnRenamed);
fsw.EnableRaisingEvents = true;
}
private void OnChanged(object source, FileSystemEventArgs e)
{
// Show that a file has been changed
WatcherChangeTypes wct = e.ChangeType;
MessageBox.Show("OnChanged File " + e.FullPath + wct.ToString());
tim.Start();// start timer as you get file.exe found....
}
private void OnCreated(object source, FileSystemEventArgs e)
{
// Show that a file has been created
WatcherChangeTypes wct = e.ChangeType;
MessageBox.Show("OnCreated File " + e.FullPath + wct.ToString());
tim.Start();// start timer as you get file.exe found....
}
private void OnDeleted(object source, FileSystemEventArgs e)
{
// Show that a file has been deleted.
WatcherChangeTypes wct = e.ChangeType;
MessageBox.Show("OnDeleted File " + e.FullPath + wct.ToString());
tim.Start();// start timer as you get file.exe found....
}
// This method is called when a file is renamed.
private void OnRenamed(object source, RenamedEventArgs e)
{
// Show that a file has been renamed.
WatcherChangeTypes wct = e.ChangeType;
MessageBox.Show("OnRenamed File " + e.OldFullPath + e.FullPath + wct.ToString());
tim.Start(); // start timer as you get file.exe found....
}
}
希望这会对你有帮助....