檔案監控練習

檔案監控練習

此練習在Visual Basic 2003-2005開發秘訣與範例大全6-18有做詳細的說明

微笑

另外補充一個參考的網址也寫不錯FileSystemWatcher 類別應用實例

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
    
        public Form1()
        {
            InitializeComponent();
        }
        private void start_Click(object sender, EventArgs e)
        {
            StreamWriter sw = File.CreateText(@"c:\recording.txt");//新增一個記事本紀錄
            sw.Dispose(); sw.Close();
            FileSystemWatcher fsw = new FileSystemWatcher(@"C:\");//指定監控目錄
            fsw.IncludeSubdirectories = true;//是否偵測子目錄的部分,相對也會增加系統的負擔
            fsw.Filter = "*.*";//偵測的檔案型態,如文字檔*.txt
            fsw.Created += new FileSystemEventHandler(fsw_Created);
            fsw.Deleted += new FileSystemEventHandler(fsw_Deleted);
            fsw.Changed += new FileSystemEventHandler(fsw_Changed);
            fsw.EnableRaisingEvents = true;//fsw 事件啟動
            start.Enabled = false;
            stop.Enabled = true;
        }
        static void fsw_Created(object sender, FileSystemEventArgs e)
        {
            Thread.Sleep(500);
            string file_data = DateTime.Now.ToString() + "\t" + e.Name + "\t" + e.ChangeType + "\t\t" + e.FullPath;
            into_txt(file_data);
        }
        static void fsw_Deleted(object sender, FileSystemEventArgs e)
        {
            Thread.Sleep(500);
            string file_data = DateTime.Now.ToString() + "\t" + e.Name + "\t" + e.ChangeType + "\t\t" + e.FullPath;
            into_txt(file_data);
        }
        static void fsw_Changed(object sender, FileSystemEventArgs e)
        {
            //if (e.Name.ToString() != "recording.txt")//這個看個人需求,開啟會因監控整個目錄輸出時會變得有點凌亂
            //{
            //    Thread.Sleep(500);
            //    string file_data = DateTime.Now.ToString() + "\t" + e.Name + "\t" + e.ChangeType + "\t\t" + e.FullPath;
            //    into_txt(file_data);
            //}
        }
        static void into_txt(string filename)//寫入記事本
        {
            StreamWriter sw = sw = new StreamWriter("C:\\recording.txt", true, Encoding.UTF8);
            try
            {
                sw.WriteLine(filename);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                sw.Dispose();
                sw.Close();
            }
        
          }

        private void stop_Click(object sender, EventArgs e)
        {
            start.Enabled = true;
            stop.Enabled = false;
            System.Diagnostics.Process.Start("C:\\recording.txt");
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            stop.Enabled = false;
        }           

    }
}

 

會寫的原因是主要為了去監控病毒到底在系統內做了什麼手腳

運用NET所提供的FileSystemWatcher 原件來完成

image

image

不過如果真的病毒再跑的話,因該挺壯觀的XD

 

 

 

 

大家一起加入blogads 賺零用錢!!