C#文件监视程序

frmMain.cs

using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Runtime.InteropServices;

namespace FileMonitor
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        [DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
        public static extern int SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam);

        static public OleDbConnection myConnect;
        //private TreeNode selNode;
        static public ArrayList folderList = new ArrayList();
        static public bool isStart = false;

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.FormClosing += new FormClosingEventHandler(frmMain_FormClosing);
            this.cTreeView.Enabled = false;
            //this.cTreeView.MouseUp += new MouseEventHandler(cTreeView_MouseUp);
            //this.cTreeView.NodeMouseClick += new TreeNodeMouseClickEventHandler(cTreeView_NodeMouseClick);
            if (!System.IO.File.Exists(Application.StartupPath + "//FileMonitor.mdb"))
            {
                MessageBox.Show("没有发现数据库文件!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Close();
                return;
            }
            if (ConnectDataBase())
            {
                //
            }
            else
            {
                MessageBox.Show("连接数据库失败!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Close();
                return;
            }
            this.twImageList.Images.Add(new Icon(Application.StartupPath + "//computer.ico"));
            TreeNode topNode = this.cTreeView.Nodes.Add("top","我的电脑",0,0);
            TreeNode subNode = new TreeNode();
            this.Resize += new EventHandler(frmMain_Resize);
            int driveCount = 0;
            int sum = 0;
            foreach (string drive in Environment.GetLogicalDrives())
            {
                if (drive.ToLower() == "a://" && drive.ToLower() != "b://")
                {
                    sum += 1;
                }
                if (drive.ToLower() != "a://" && drive.ToLower() != "b://")
                {
                    System.IO.DriveInfo myDriveInfo = new System.IO.DriveInfo(drive);
                    if (myDriveInfo.DriveType != System.IO.DriveType.CDRom && myDriveInfo.DriveType != System.IO.DriveType.Unknown && myDriveInfo.DriveType != System.IO.DriveType.NoRootDirectory)
                    {
                        string volumeLabel = myDriveInfo.VolumeLabel;
                        if (!frmEdit.DateIsEmpty("NoDriveMonitorInfo", drive, "NoMonitorDrive"))
                        {
                            driveCount += 1;
                            if (volumeLabel != "")
                            {
                                if (myDriveInfo.DriveType == System.IO.DriveType.Removable)
                                {
                                    AddToTreeView(Application.StartupPath + "//u.ico", drive, "可移动磁盘" + "(" + drive.Substring(0, 2) + ")", true, ref topNode, ref subNode);
                                }
                                else
                                {
                                    AddToTreeView(Application.StartupPath + "//disk.ico", drive, volumeLabel + "(" + drive.Substring(0, 2) + ")", true, ref topNode, ref subNode);
                                }
                            }
                            else
                            {
                                if (myDriveInfo.DriveType == System.IO.DriveType.Removable)
                                {
                                    AddToTreeView(Application.StartupPath + "//u.ico", drive, "可移动磁盘" + "(" + drive.Substring(0, 2) + ")", true, ref topNode, ref subNode);
                                }
                                else
                                {
                                    AddToTreeView(Application.StartupPath + "//disk.ico", drive, "本地磁盘" + "(" + drive.Substring(0, 2) + ")", true, ref topNode, ref subNode);
                                }
                            }
                            classWatcher myFileWatcher = new classWatcher(this.lv, drive, "*.*", true);
                        }
                        else
                        {
                            if (volumeLabel != "")
                            {
                                if (myDriveInfo.DriveType == System.IO.DriveType.Removable)
                                {
                                    AddToTreeView(Application.StartupPath + "//u.ico", drive, "可移动磁盘" + "(" + drive.Substring(0, 2) + ")", false, ref topNode, ref subNode);
                                }
                                else
                                {
                                    AddToTreeView(Application.StartupPath + "//disk.ico", drive, volumeLabel + "(" + drive.Substring(0, 2) + ")", false, ref topNode, ref subNode);
                                }
                            }
                            else
                            {
                                if (myDriveInfo.DriveType == System.IO.DriveType.Removable)
                                {
                                    AddToTreeView(Application.StartupPath + "//u.ico", drive, "可移动磁盘" + "(" + drive.Substring(0, 2) + ")", false, ref topNode, ref subNode);
                                }
                                else
                                {
                                    AddToTreeView(Application.StartupPath + "//disk.ico", drive, "本地磁盘" + "(" + drive.Substring(0, 2) + ")", false, ref topNode, ref subNode);
                                }
                            }
                        }
                        if (Environment.GetLogicalDrives().Length == (driveCount + sum))
                        {
                            topNode.Checked = true;
                        }
                        topNode.Expand();
                    }
                }
            }
           
            if (frmEdit.DateIsEmpty("NoCheckFolderInfo", "", "NoCheckFolderPath"))
            {
                OleDbCommand sCommand = new OleDbCommand("Select * From NoCheckFolderInfo", myConnect);
                OleDbDataReader sDataReader = sCommand.ExecuteReader();
                while (sDataReader.Read())
                {
                    folderList.Add(sDataReader.GetValue(1).ToString());
                    NoCheckList.Items.Add(sDataReader.GetValue(1).ToString());
                    SendMessage(this.NoCheckList.Handle, 0x115, 7, 0);
                }
                sDataReader.Close();
                sDataReader.Dispose();
                sCommand.Dispose();
            }
            isStart = true;
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.IO.FileSystemWatcher myWatcher;
            foreach (object watcher in classWatcher.myArrayList)
            {
                myWatcher = (System.IO.FileSystemWatcher)watcher;
                classWatcher.StopFileWatcher(myWatcher.Path);
            }
        }

        //private void cTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        //{
        //    selNode = e.Node;
        //}

        //private void cTreeView_MouseUp(object sender, MouseEventArgs e)
        //{
        //    int subNodeCount = cTreeView.GetNodeCount(true) - 1;
        //    if (selNode.Text == "我的电脑")
        //    {
        //        if (selNode.Checked)
        //        {
        //            TreeNode topNode = new TreeNode();
        //            topNode = cTreeView.Nodes[0];
        //            TreeNodeCollection nodes = topNode.Nodes;
        //            foreach (TreeNode node in nodes)
        //            {
        //                node.Checked = true;
        //            }
        //        }
        //        else
        //        {
        //            this.cTreeView.Nodes[0].Checked = false;
        //            TreeNode topNode = new TreeNode();
        //            topNode = cTreeView.Nodes[0];
        //            TreeNodeCollection nodes = topNode.Nodes;
        //            foreach (TreeNode node in nodes)
        //            {
        //                node.Checked = false;
        //            }
        //        }
        //    }
        //    else
        //    {
        //        if (GetCheckNodes(this.cTreeView, subNodeCount))
        //        {
        //            this.cTreeView.Nodes[0].Checked = true;
        //        }
        //        else this.cTreeView.Nodes[0].Checked = false;
        //    }
        //}

        static public bool GetCheckNodes(TreeView tw,int nodeCount)
        {
            int sum = 0;
            TreeNode topNode = new TreeNode();
            topNode = tw.Nodes[0];
            TreeNodeCollection nodes = topNode.Nodes;
            foreach (TreeNode node in nodes)
            {
                if (node.Checked)
                {
                    sum += 1;
                }
            }
            if (sum == nodeCount)
            {
                return true;
            }
            else return false;

        }

        private void AddToTreeView(string icoPath ,string key, string diskVolumeLabel,bool isChecked,ref TreeNode topNode, ref TreeNode addNode)
        {
            this.twImageList.Images.Add(new Icon(icoPath));
            addNode = topNode.Nodes.Add(key, diskVolumeLabel, this.twImageList.Images.Count - 1, this.twImageList.Images.Count - 1);
            addNode.Checked = isChecked;
        }

        private void frmMain_Resize(object sender, EventArgs e)
        {
            this.lv.Width = this.Width - 30 - this.cTreeView.Width;
            if (lv.Height == 366)
            {
                this.lv.Height = this.Height - 91;
                this.cmdExit.Location = new Point(this.Width - 75, this.Height - 69);
                this.cmdClear.Location = new Point(this.Width - 75 - this.cmdExit.Width - 5, this.Height - 69);
                this.cmdEdit.Location = new Point(this.Width - 75 - this.cmdClear.Width * 2 - 10, this.Height - 69);
                this.cmdView.Location = new Point(this.Width - 75 - this.cmdClear.Width * 3 - 15, this.Height - 69);
            }
            else
            {
                this.lv.Height = 366;
                this.cmdExit.Location = new Point(541, 385);
                this.cmdClear.Location = new Point(409, 385);
                this.cmdEdit.Location = new Point(475, 385);
                this.cmdView.Location = new Point(343, 385);
            }
            this.NoCheckList.Height = this.lv.Height - this.cTreeView.Height - 38;
        }

        private bool ConnectDataBase()
        {
            try
            {
                myConnect = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Application.StartupPath + "//FileMonitor.mdb");
                myConnect.Open();
                return true;
            }
            catch { return false; }
        }

        private void cmdClear_Click(object sender, EventArgs e)
        {
            this.lv.Items.Clear();
        }

        private void cmdEdit_Click(object sender, EventArgs e)
        {
            frmEdit fm = new frmEdit(this.lv, this.cTreeView, this.twImageList,this.NoCheckList);
            fm.Show();
        }

        private void cmdExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmdView_Click(object sender, EventArgs e)
        {
            frmView fm = new frmView();
            fm.Show();
        }
    }

    public class classWatcher
    {
        static public ArrayList myArrayList = new ArrayList();
        private System.IO.FileSystemWatcher myWatcher = new System.IO.FileSystemWatcher();
        private ListView myListView;

        public classWatcher(ListView myLv, string strPath, string strFilter, bool includeSubdirectories)
        {
            if (!FileWatcherIsIn(strPath))
            {
                this.myListView = myLv;
                myWatcher.Path = strPath;
                myWatcher.Filter = strFilter;
                myWatcher.InternalBufferSize = myWatcher.InternalBufferSize * 2;
                myWatcher.IncludeSubdirectories = includeSubdirectories;
                myWatcher.EnableRaisingEvents = true;
                myWatcher.Created += new System.IO.FileSystemEventHandler(myWatcher_Created);
                myWatcher.Changed += new System.IO.FileSystemEventHandler(myWatcher_Changed);
                myWatcher.Deleted += new System.IO.FileSystemEventHandler(myWatcher_Deleted);
                myWatcher.Renamed += new System.IO.RenamedEventHandler(myWatcher_Renamed);
                myArrayList.Add(myWatcher);
            }
        }

        private void myWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            try
            {
                if (frmMain.isStart)
                {
                    if (frmMain.folderList.IndexOf(System.IO.Path.GetDirectoryName(e.FullPath)) < 0)
                    {
                        if ((System.IO.File.GetAttributes(e.FullPath) & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory)
                        {
                            AddToListView(DateTime.Now.ToString(), Environment.UserName, e.FullPath, e.FullPath + " 文件夹被创建", "创建文件夹");
                        }
                        else AddToListView(DateTime.Now.ToString(), Environment.UserName, e.FullPath, e.FullPath + " 文件被创建", "创建文件");
                    }
                }
            }
            catch { }
        }

        private void myWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
            try
            {
                if (frmMain.isStart)
                {
                    if (frmMain.folderList.IndexOf(System.IO.Path.GetDirectoryName(e.FullPath)) < 0)
                    {
                        if ((System.IO.File.GetAttributes(e.FullPath) & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory)
                        {
                            AddToListView(DateTime.Now.ToString(), Environment.UserName, e.FullPath, e.FullPath + " 文件夹内容修改", "修改文件夹");
                        }
                        else AddToListView(DateTime.Now.ToString(), Environment.UserName, e.FullPath, e.FullPath + " 文件内容修改", "修改文件");
                    }
                }
            }
            catch { }
        }

        private void myWatcher_Deleted(object sender, System.IO.FileSystemEventArgs e)
        {
            try
            {
                if (frmMain.isStart)
                {
                    if (frmMain.folderList.IndexOf(System.IO.Path.GetDirectoryName(e.FullPath)) < 0)
                    {
                        AddToListView(DateTime.Now.ToString(), Environment.UserName, e.FullPath, e.FullPath + " 文件/目录被删除", "删除文件/目录");
                    }
                }
            }
            catch { }
        }

        private void myWatcher_Renamed(object sender, System.IO.RenamedEventArgs e)
        {
            try
            {
                if (frmMain.isStart)
                {
                    if (frmMain.folderList.IndexOf(System.IO.Path.GetDirectoryName(e.FullPath)) < 0)
                    {
                        if ((System.IO.File.GetAttributes(e.FullPath) & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory)
                        {
                            AddToListView(DateTime.Now.ToString(), Environment.UserName, e.OldFullPath, e.OldFullPath + " 被重命名为 " + e.FullPath, "重命名文件夹");
                        }
                        else AddToListView(DateTime.Now.ToString(), Environment.UserName, e.OldFullPath, e.OldFullPath + " 被重命名为 " + e.FullPath, "重命名文件");
                    }
                }
            }
            catch { }
        }

        static public void StopFileWatcher(string strPath)
        {
            try
            {
                System.IO.FileSystemWatcher sWatcher;
                foreach (object fWatcher in myArrayList)
                {
                    sWatcher = (System.IO.FileSystemWatcher)fWatcher;
                    if (sWatcher.Path.ToLower() == strPath.ToLower()) sWatcher.EnableRaisingEvents = false;
                }
            }
            catch { }
        }

        static public void RestoreFileWatcher(string strPath)
        {
            try
            {
                System.IO.FileSystemWatcher sWatcher;
                foreach (object fWatcher in myArrayList)
                {
                    sWatcher = (System.IO.FileSystemWatcher)fWatcher;
                    if (sWatcher.Path.ToLower() == strPath.ToLower())
                    {
                        if (!sWatcher.EnableRaisingEvents) sWatcher.EnableRaisingEvents = true;
                    }
                }
            }
            catch { }
        }

        private bool FileWatcherIsIn(string strPath)
        {
            bool isIn=false;
            System.IO.FileSystemWatcher sWatcher;
            try
            {
                foreach (object fWatcher in myArrayList)
                {
                    sWatcher = (System.IO.FileSystemWatcher)fWatcher;
                    if (sWatcher.Path.ToLower() == strPath.ToLower())
                    {
                        isIn = true;
                        sWatcher.EnableRaisingEvents = true;
                    }
                }
            }
            catch { }
            return isIn;
        }

        private void AddToListView(string strTime,string strUser,string strPath,string strEvent,string strType)
        {
            ListViewItem item = new ListViewItem();
            item = myListView.Items.Add(strTime);
            item.SubItems.AddRange(new string[] { strUser, strPath, strEvent });
            frmMain.SendMessage(this.myListView.Handle, 0x115, 7, 0);
            OleDbCommand myOleDbCommand = new OleDbCommand("Insert Into EventInfo (sTime,sUser,sPath,sEvent,sType) Values('" + strTime + "','" + strUser + "','" + strPath + "','" + strEvent + "','" + strType + "')", frmMain.myConnect);
            myOleDbCommand.ExecuteNonQuery();
        }
    }
}

frmEdit.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace FileMonitor
{
    public partial class frmEdit : Form
    {
        private ListView lsv;
        private TreeView ctv;
        private ImageList imgList;
        private ListBox lt;

        public frmEdit(ListView lv,TreeView tw,ImageList iList,ListBox list)
        {
            this.lsv = lv;
            this.ctv = tw;
            this.imgList = iList;
            this.lt = list;
            InitializeComponent();
        }

        private TreeNode selNode;
        private bool isAddDataBase = false;
        private bool isDeleteDataBase = false;

        private void frmEdit_Load(object sender, EventArgs e)
        {
            this.FormClosing += new FormClosingEventHandler(frmEdit_FormClosing);
            this.cTreeView.MouseUp += new MouseEventHandler(cTreeView_MouseUp);
            this.cTreeView.NodeMouseClick += new TreeNodeMouseClickEventHandler(cTreeView_NodeMouseClick);
            this.twImageList.Images.Add(new Icon(Application.StartupPath + "//computer.ico"));
            TreeNode topNode = this.cTreeView.Nodes.Add("top", "我的电脑", 0, 0);
            TreeNode subNode = new TreeNode();
            int driveCount = 0;
            int sum = 0;
            foreach (string drive in Environment.GetLogicalDrives())
            {
                if (drive.ToLower() == "a://" && drive.ToLower() != "b://")
                {
                    sum += 1;
                }
                if (drive.ToLower() != "a://" && drive.ToLower() != "b://")
                {
                    System.IO.DriveInfo myDriveInfo = new System.IO.DriveInfo(drive);
                    if (myDriveInfo.DriveType != System.IO.DriveType.CDRom && myDriveInfo.DriveType != System.IO.DriveType.Unknown && myDriveInfo.DriveType != System.IO.DriveType.NoRootDirectory)
                    {
                        string volumeLabel = myDriveInfo.VolumeLabel;
                        TreeNode addNode = new TreeNode();
                        if (this.ctv.Nodes[0].Nodes.Find(drive, true).GetLength(0)== 0)
                        {
                            if (myDriveInfo.DriveType == System.IO.DriveType.Removable)
                            {
                                this.imgList.Images.Add(new Icon(Application.StartupPath + "//u.ico"));
                                addNode=this.ctv.Nodes[0].Nodes.Add(drive, "可移动磁盘" + "(" + drive.Substring(0, 2) + ")", this.imgList.Images.Count - 1, this.imgList.Images.Count - 1);
                                addNode.Checked = true;
                            }
                            else
                            {
                                this.imgList.Images.Add(new Icon(Application.StartupPath + "//disk.ico"));
                                addNode=this.ctv.Nodes[0].Nodes.Add(drive, "本地磁盘" + "(" + drive.Substring(0, 2) + ")", this.imgList.Images.Count - 1, this.imgList.Images.Count - 1);
                                addNode.Checked = true;
                            }
                        }
                        if (!DateIsEmpty("NoDriveMonitorInfo", drive, "NoMonitorDrive"))
                        {
                            driveCount += 1;
                            if (volumeLabel != "")
                            {
                                if (myDriveInfo.DriveType == System.IO.DriveType.Removable)
                                {
                                    AddToTreeView(Application.StartupPath + "//u.ico", drive, "可移动磁盘" + "(" + drive.Substring(0, 2) + ")", true, ref topNode, ref subNode);
                                }
                                else
                                {
                                    AddToTreeView(Application.StartupPath + "//disk.ico", drive, volumeLabel + "(" + drive.Substring(0, 2) + ")", true, ref topNode, ref subNode);
                                }
                            }
                            else
                            {
                                if (myDriveInfo.DriveType == System.IO.DriveType.Removable)
                                {
                                    AddToTreeView(Application.StartupPath + "//u.ico", drive, "可移动磁盘" + "(" + drive.Substring(0, 2) + ")", true, ref topNode, ref subNode);
                                }
                                else
                                {
                                    AddToTreeView(Application.StartupPath + "//disk.ico", drive, "本地磁盘" + "(" + drive.Substring(0, 2) + ")", true, ref topNode, ref subNode);
                                }
                            }
                        }
                        else
                        {
                            if (volumeLabel != "")
                            {
                                if (myDriveInfo.DriveType == System.IO.DriveType.Removable)
                                {
                                    AddToTreeView(Application.StartupPath + "//u.ico", drive, "可移动磁盘" + "(" + drive.Substring(0, 2) + ")", false, ref topNode, ref subNode);
                                }
                                else
                                {
                                    AddToTreeView(Application.StartupPath + "//disk.ico", drive, volumeLabel + "(" + drive.Substring(0, 2) + ")", false, ref topNode, ref subNode);
                                }
                            }
                            else
                            {
                                if (myDriveInfo.DriveType == System.IO.DriveType.Removable)
                                {
                                    AddToTreeView(Application.StartupPath + "//u.ico", drive, "可移动磁盘" + "(" + drive.Substring(0, 2) + ")", false, ref topNode, ref subNode);
                                }
                                else
                                {
                                    AddToTreeView(Application.StartupPath + "//disk.ico", drive, "本地磁盘" + "(" + drive.Substring(0, 2) + ")", false, ref topNode, ref subNode);
                                }
                            }
                        }
                        if (Environment.GetLogicalDrives().Length == (driveCount + sum))
                        {
                            topNode.Checked = true;
                        }
                        topNode.Expand();
                    }
                }
            }
            if (DateIsEmpty("NoCheckFolderInfo", "", "NoCheckFolderPath"))
            {
                cmdRefresh_Click(new object(), new EventArgs());
            }
        }

        private void frmEdit_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.isAddDataBase)
            {
                MessageBox.Show("程序正在添加数据中,不能退出!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
            if (this.isDeleteDataBase)
            {
                MessageBox.Show("程序删除数据中,不能退出!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void GetAllFolders(string RootPath)
        {
            System.IO.DirectoryInfo myDirectoryInfo = new System.IO.DirectoryInfo(RootPath);
            System.IO.DirectoryInfo[] folders = myDirectoryInfo.GetDirectories();
            foreach (System.IO.DirectoryInfo folder in folders)
            {
                Application.DoEvents();
                if (!DateIsEmpty("NoCheckFolderInfo", folder.FullName, "NoCheckFolderPath"))
                {
                    this.Text = "正在添加 :" + folder.FullName;
                    NoCheckList.Items.Add(folder.FullName);
                    this.lt.Items.Add(textPath.Text);
                    frmMain.SendMessage(this.lt.Handle, 0x115, 7, 0);
                    frmMain.folderList.Add(folder.FullName);
                    frmMain.SendMessage(this.NoCheckList.Handle, 0x115, 7, 0);
                    AddToDataBase("NoCheckFolderInfo", folder.FullName, "NoCheckFolderPath");
                }
                GetAllFolders(folder.FullName);
            }
        }

        private void AddToTreeView(string icoPath, string key, string diskVolumeLabel, bool isChecked, ref TreeNode topNode, ref TreeNode addNode)
        {
            this.twImageList.Images.Add(new Icon(icoPath));
            addNode = topNode.Nodes.Add(key, diskVolumeLabel, this.twImageList.Images.Count - 1, this.twImageList.Images.Count - 1);
            addNode.Checked = isChecked;
        }

        private void cmdExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SetTreeViewState(TreeNode node, bool state)
        {
            if (node.FullPath == "我的电脑")
            {
                this.ctv.Nodes[0].Checked = state;
                return;
            }
            TreeNode[] nodes = this.ctv.Nodes[0].Nodes.Find(node.Text.Substring(node.Text.IndexOf("(") + 1, 2) + "//", true);
            if (nodes.GetLength(0) > 0)
            {
                nodes[0].Checked = state;
            }
        }

        private void cmdOk_Click(object sender, EventArgs e)
        {
            TreeNode topNode = new TreeNode();
            topNode = cTreeView.Nodes[0];
            TreeNodeCollection nodes = topNode.Nodes;
            foreach (TreeNode node in nodes)
            {
                if (node.Checked == false)
                {
                    SetTreeViewState(node, false);
                    if (!DateIsEmpty("NoDriveMonitorInfo", node.Text.Substring(node.Text.IndexOf("(") + 1, 2) + "//", "NoMonitorDrive"))
                    {
                        AddToDataBase("NoDriveMonitorInfo", node.Text.Substring(node.Text.IndexOf("(") + 1, 2) + "//", "NoMonitorDrive");
                    }
                    //else DeleteDataBase("NoDriveMonitorInfo", node.Text.Substring(node.Text.IndexOf("(") + 1, 2) + "//", "NoMonitorDrive");
                    classWatcher.StopFileWatcher(node.Text.Substring(node.Text.IndexOf("(") + 1, 2) + "//");
                }
                else
                {
                    SetTreeViewState(node, true);
                    if (DateIsEmpty("NoDriveMonitorInfo", node.Text.Substring(node.Text.IndexOf("(") + 1, 2) + "//", "NoMonitorDrive"))
                    {
                        DeleteDataBase("NoDriveMonitorInfo", node.Text.Substring(node.Text.IndexOf("(") + 1, 2) + "//", "NoMonitorDrive");
                    }
                    classWatcher myclassWatcher = new classWatcher(this.lsv, node.Text.Substring(node.Text.IndexOf("(") + 1, 2) + "//", "*.*", true);
                }
            }
            if (topNode.Checked)
            {
                SetTreeViewState(topNode, true);
            }
            else SetTreeViewState(topNode, false);
            MessageBox.Show("设置成功!!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void cTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            selNode = e.Node;
        }

        private void cTreeView_MouseUp(object sender, MouseEventArgs e)
        {
            int subNodeCount = cTreeView.GetNodeCount(true) - 1;
            if (selNode.Text == "我的电脑")
            {
                if (selNode.Checked)
                {
                    TreeNode topNode = new TreeNode();
                    topNode = cTreeView.Nodes[0];
                    TreeNodeCollection nodes = topNode.Nodes;
                    foreach (TreeNode node in nodes)
                    {
                        node.Checked = true;
                    }
                }
                else
                {
                    this.cTreeView.Nodes[0].Checked = false;
                    TreeNode topNode = new TreeNode();
                    topNode = cTreeView.Nodes[0];
                    TreeNodeCollection nodes = topNode.Nodes;
                    foreach (TreeNode node in nodes)
                    {
                        node.Checked = false;
                    }
                }
            }
            else
            {
                if (frmMain.GetCheckNodes(this.cTreeView, subNodeCount))
                {
                    this.cTreeView.Nodes[0].Checked = true;
                    SetTreeViewState(this.ctv.Nodes[0], true);
                }
                else
                {
                    this.cTreeView.Nodes[0].Checked = false;
                    SetTreeViewState(this.ctv.Nodes[0], false);
                }
            }
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            SetButtonState(false);
            isAddDataBase = true;
            if (textPath.Text != "")
            {
                if (textPath.Text.IndexOf(":") > 0 && textPath.Text.Length > 3)
                {
                    if (this.checkSub.Checked)
                    {
                        if (!System.IO.Directory.Exists(textPath.Text))
                        {
                            if (DateIsEmpty("NoCheckFolderInfo", textPath.Text, "NoCheckFolderPath"))
                            {
                                MessageBox.Show("已经添加了此路径!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                SetButtonState(true);
                                isAddDataBase = false;
                                this.checkSub.Checked = false;
                                return;
                            }
                            else
                            {
                                NoCheckList.Items.Add(textPath.Text);
                                this.lt.Items.Add(textPath.Text);
                                frmMain.SendMessage(this.lt.Handle, 0x115, 7, 0);
                                frmMain.folderList.Add(textPath.Text);
                                frmMain.SendMessage(this.NoCheckList.Handle, 0x115, 7, 0);
                                AddToDataBase("NoCheckFolderInfo", textPath.Text, "NoCheckFolderPath");
                            }
                        }
                        else
                        {
                            if (!DateIsEmpty("NoCheckFolderInfo", textPath.Text, "NoCheckFolderPath"))
                            {
                                this.Text="正在添加 :" + textPath.Text;
                                NoCheckList.Items.Add(textPath.Text);
                                this.lt.Items.Add(textPath.Text);
                                frmMain.SendMessage(this.lt.Handle, 0x115, 7, 0);
                                frmMain.folderList.Add(textPath.Text);
                                frmMain.SendMessage(this.NoCheckList.Handle, 0x115, 7, 0);
                                AddToDataBase("NoCheckFolderInfo", textPath.Text, "NoCheckFolderPath");
                            }
                            GetAllFolders(textPath.Text);
                            this.Text = "监视编辑";
                        }
                    }
                    else
                    {
                        if (!DateIsEmpty("NoCheckFolderInfo", textPath.Text, "NoCheckFolderPath"))
                        {
                            NoCheckList.Items.Add(textPath.Text);
                            this.lt.Items.Add(textPath.Text);
                            frmMain.SendMessage(this.lt.Handle, 0x115, 7, 0);
                            frmMain.folderList.Add(textPath.Text);
                            frmMain.SendMessage(this.NoCheckList.Handle, 0x115, 7, 0);
                            AddToDataBase("NoCheckFolderInfo", textPath.Text, "NoCheckFolderPath");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("路径不能为系统分区!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    SetButtonState(true);
                    isAddDataBase = false;
                    return;
                }
            }
            else
            {
                MessageBox.Show("路径不能为空!!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                SetButtonState(true);
                isAddDataBase = false;
                return;
            }
            textPath.Text = "";
            SetButtonState(true);
            isAddDataBase = false;
            this.checkSub.Checked = false;
            MessageBox.Show("添加成功!!","成功");
        }

        private void SetButtonState(bool isTrue)
        {
            if (!isTrue)
            {
                cmdAdd.Enabled = false;
                cmdClear.Enabled = false;
                cmdDelete.Enabled = false;
                cmdExit.Enabled = false;
                cmdPath.Enabled = false;
                cmdRefresh.Enabled = false;
                cmdOk.Enabled = false;
            }
            else
            {
                cmdAdd.Enabled = true;
                cmdClear.Enabled = true;
                cmdDelete.Enabled = true;
                cmdExit.Enabled = true;
                cmdPath.Enabled = true;
                cmdRefresh.Enabled = true;
                cmdOk.Enabled = true;
            }
        }

        private void cmdPath_Click(object sender, EventArgs e)
        {
            this.folderDlg.SelectedPath = "";
            this.folderDlg.Description = "请选择您不需要监视的目录:";
            this.folderDlg.ShowDialog();
            if (this.folderDlg.SelectedPath != "")
            {
                textPath.Text = this.folderDlg.SelectedPath;
            }
        }

        static public bool DateIsEmpty(string sTab,string strPath,string sValue)
        {
            bool isIn = false;
            OleDbCommand myOleDbCommand = new OleDbCommand("Select * From " + sTab, frmMain.myConnect);
            OleDbDataReader myDataReader = myOleDbCommand.ExecuteReader();
            if (!myDataReader.Read())
            {
                isIn = false;
            }
            else isIn = true;
            myDataReader.Close();
            myDataReader.Dispose();
            myOleDbCommand.Dispose();
           
            if (isIn && strPath!="")
            {
                string strSQL = "Select * From " + sTab + " Where " + sValue + "='" + strPath + "'";
                OleDbCommand checkIn = new OleDbCommand(strSQL, frmMain.myConnect);
                OleDbDataReader myReader = checkIn.ExecuteReader(CommandBehavior.SingleRow);
                if (!myReader.Read())
                {
                    isIn = false;
                }
                else isIn = true;
                checkIn.Dispose();
            }
            return isIn;
        }

        private void AddToDataBase(string tab, string strPath,string value)
        {
            OleDbCommand myOleDbCommand = new OleDbCommand("Insert Into " + tab + " (" + value + ") Values('" + strPath + "')" , frmMain.myConnect);
            myOleDbCommand.ExecuteNonQuery();
            myOleDbCommand.Dispose();
        }

        private void UpdateDataBase(string tab, string strPath, string value)
        {
            OleDbCommand myOleDbCommand = new OleDbCommand("Update " + tab + " Set " + value + "='" + strPath + "'" , frmMain.myConnect);
            myOleDbCommand.ExecuteNonQuery();
            myOleDbCommand.Dispose();
        }

        private void DeleteDataBase(string tab, string strPath, string value)
        {
            OleDbCommand myOleDbCommand = new OleDbCommand("Delete From " + tab + " Where " + value + "='" + strPath + "'", frmMain.myConnect);
            myOleDbCommand.ExecuteNonQuery();
            myOleDbCommand.Dispose();
        }

        private void cmdRefresh_Click(object sender, EventArgs e)
        {
            this.NoCheckList.Items.Clear();
            OleDbCommand myOleDbCommand = new OleDbCommand("Select * From NoCheckFolderInfo", frmMain.myConnect);
            OleDbDataReader myReader = myOleDbCommand.ExecuteReader();
            while (myReader.Read())
            {
                Application.DoEvents();
                this.NoCheckList.Items.Add(myReader.GetValue(1).ToString());
                frmMain.SendMessage(this.NoCheckList.Handle, 0x115, 7, 0);
            }
        }

        private void cmdClear_Click(object sender, EventArgs e)
        {
            this.NoCheckList.Items.Clear();
        }

        private void cmdDelete_Click(object sender, EventArgs e)
        {
            isDeleteDataBase = true;
            SetButtonState(false);
            ListBox.SelectedIndexCollection myCollection = this.NoCheckList.SelectedIndices;
            switch (myCollection.Count)
            {
                case 0:
                    break;
                case 1:
                    frmMain.folderList.Remove(this.NoCheckList.Items[this.NoCheckList.SelectedIndex].ToString());
                    DeleteDataBase("NoCheckFolderInfo", this.NoCheckList.Items[this.NoCheckList.SelectedIndex].ToString(), "NoCheckFolderPath");
                    this.NoCheckList.Items.RemoveAt(this.NoCheckList.SelectedIndex);
                    MessageBox.Show("删除成功!!","成功");
                    break;
                default:
                    if (myCollection.Count > 1)
                    {
                        for (int i = myCollection.Count-1; i >= 0; i--)
                        {
                            frmMain.folderList.Remove(this.NoCheckList.Items[this.NoCheckList.SelectedIndices[i]].ToString());
                            DeleteDataBase("NoCheckFolderInfo", this.NoCheckList.Items[this.NoCheckList.SelectedIndices[i]].ToString(), "NoCheckFolderPath");
                            this.NoCheckList.Items.RemoveAt(this.NoCheckList.SelectedIndices[i]);
                        }
                        MessageBox.Show("删除成功!!", "成功");
                    }
                    break;
            }
            isDeleteDataBase = false;
            SetButtonState(true);
        }
    }
}

frmView.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace FileMonitor
{
    public partial class frmView : Form
    {
        public frmView()
        {
            InitializeComponent();
        }

        private int iWidth;
        private int iHeight;
        private int nowWidth;
        private int nowHeight;

        private void frmView_Load(object sender, EventArgs e)
        {
            iWidth = 531;
            iHeight = 401;
            this.Resize += new EventHandler(frmView_Resize);
            RefreshDataBase();
        }

        private void frmView_Resize(object sender, EventArgs e)
        {
            nowWidth = this.Width;
            nowHeight = this.Height;
            this.dataGridView.Width = this.dataGridView.Width + (nowWidth - iWidth);
            this.dataGridView.Height = this.dataGridView.Height + (nowHeight - iHeight);
            this.cmdExit.Location = new Point(this.cmdExit.Location.X + (nowWidth - iWidth), this.cmdExit.Location.Y + (nowHeight - iHeight));
            this.cmdExport.Location = new Point(this.cmdExport.Location.X + (nowWidth - iWidth), this.cmdExport.Location.Y + (nowHeight - iHeight));
            this.cmdRefresh.Location = new Point(this.cmdRefresh.Location.X + (nowWidth - iWidth), this.cmdRefresh.Location.Y + (nowHeight - iHeight));
            iWidth = this.Width;
            iHeight = this.Height;
        }

        private void RefreshDataBase()
        {
            string strSQL = "Select sTime As 时间,sUser As 用户,sPath As 路径,sEvent As 事件,sType As 类型 From EventInfo";
            OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(strSQL, frmMain.myConnect);
            DataSet myDataSet = new DataSet();
            myDataAdapter.Fill(myDataSet, "EventInfo");
            dataGridView.DataSource = myDataSet.Tables["EventInfo"].DefaultView;
            myDataAdapter.Dispose();
            myDataSet.Dispose();
            dataGridView.Refresh();
        }

        private void cmdRefresh_Click(object sender, EventArgs e)
        {
            RefreshDataBase();
        }

        private void cmdExport_Click(object sender, EventArgs e)
        {

        }

        private void cmdExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值