C#文件监视程序

原创 2007年10月02日 15:51:00

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();
        }
    }

一个c#即时监控小程序

关键词:委托  线程  异步操作  大数据存储过程分页  实时刷新界面数据  声音报警 任务栏提示   动态任务栏图标切换    需求:启动监控程序后,每隔10秒(可配置多少秒)从后台数据库查询...

C#写的文件监视程序

  • 2009年08月21日 10:28
  • 39KB
  • 下载

C#关联程序的文件路径中存在空格的问题解决

今天测试的时候无意间发现,打开通过写注册表关联了程序的文件,如果文件名中存在空格,则无法通过双击打开。 经测试发现,原来是在Main方法入口点传入的参数为string[] 类型,当传入带空格的路径时...

C#调用exe文件,IIS发布后无法掉用本地exe程序的解决方法

webservice发布到IIS后process類无法調用本地exe程序的解决方法 修改C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/CONFIG目錄下...

C# 程序如何操作linux服务器上的文件

刚开始思考这个问题的时候,完全没有头绪,不知道怎么跨越系统的屏障来操作linux服务器上的文件,还好大神很多,度娘了一下,发现其实跟系统没关系,只要是想操作文件,FTP,HTTP等都可以。 下面把简...
  • jly_ang
  • jly_ang
  • 2017年03月31日 14:10
  • 664

[C# 网络编程系列]专题十一:实现一个基于FTP协议的程序——文件上传下载器

引言: 在这个专题将为大家揭开下FTP这个协议的面纱,其实学习知识和生活中的例子都是很相通的,就拿这个专题来说,要了解FTP协议然后根据FTP协议实现一个文件下载器,就和和追MM是差不多的过程的...

C#程序实现动态调用非托管的DLL文件

C#程序实现动态调用非托管的DLL文件 最近的一个项目需要用C#调用C++的Dll,由于C++的Dll是非托管的,所以调用时需要一些技巧。下面分享给新手。   一.Dll文件   动态链接...

用C#实现一个基于http的文件发布程序

用C#实现的一个基于http的文件发布程序,可用于共享文件将此程序和config.config配置文件放在需要共享的文件所在的文件夹下;将本机ip和要监听的端口添加到config.config中(可添...
  • changhu
  • changhu
  • 2011年07月27日 10:53
  • 2766

SilverLight C#程序之:读取并修改App.config文件

1. 向项目添加app.config文件:右击项目名称,选择“添加”→“添加新建项”,在出现的“添加新项”对话框中,选择“添加应用程序配置文件”;如果项目以前没有配置文件,则默认的文件名称为“app....

C#程序执行EXE文件

原文地址:http://blog.163.com/qiaoweipeng@126/blog/static/15737970200862331842368/ C#程序执行EXE文件(调用Del...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C#文件监视程序
举报原因:
原因补充:

(最多只允许输入30个字)