-4

大家好,

//请我有一个简单的问题。我需要,当 Filesystemwatcher 看到 File -after fsw 启动关闭应用程序的计时器(应用程序将在 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();         
    }




}

}

但是谢谢大家;-)

4

1 回答 1

2

我认为您想创建一个监视您指定的路径并在找到“ file.exeFileSystemWatcher ”时给您一个事件。一旦您的程序发现此文件计时器开始启动,并在几次(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....
        }
    }

希望对你有帮助....

于 2013-05-20T12:12:18.907 回答