Exemplo n.º 1
0
        //开始一个监视
        public void StartWatch(object sender, EventArgs e)
        {
            XWXFileSystemWatcher watcher = GetSelectedWatcher(true, "请选择需要开始的监视项");

            if (watcher != null)
            {
                watcher.StartWatch();
                //开始监视后,将监视动作设置为可编辑
                dgrdTaskList.SelectedRows[0].Cells[IDX_CHANGETYPE].ReadOnly = false;
                dgrdTaskList.SelectedRows[0].Cells[IDX_SUBDIR].ReadOnly     = false;
                //当开始一个监视后触发OnWatchSettingChanged事件以便进行实时保存
                this.OnWatchSettingChanged();
            }
        }
Exemplo n.º 2
0
        //从一个WatchInfo载入一个监视项到界面中,并重现该WatchInfo的信息
        private void AddWatchItem(WatchInfo watch)
        {
            XWXFileSystemWatcher watcher = new XWXFileSystemWatcher(watch.WatchedFolder);

            watcher.BackupFolder = watch.BackupFolder;
            watcher.NotifyFilter = NotifyFilters.Security
                                   | NotifyFilters.LastWrite
                                   | NotifyFilters.FileName
                                   | NotifyFilters.DirectoryName
                                   | NotifyFilters.Size
                                   | NotifyFilters.Attributes | NotifyFilters.CreationTime;

            watcher.OnWatchStatusChanged += new WatchStatusChangedHandler(watcher_OnWatchStatusChanged);

            //目前版本不监视删除和重命名操作
            //watcher.Deleted += new FileSystemEventHandler(watcher_Deleted);
            //watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
            if ((watch.ChangeTypes & WatcherChangeTypes.Renamed) == WatcherChangeTypes.Renamed)
            {
                watcher.Renamed            += new RenamedEventHandler(watcher_Renamed);
                watcher.WatchingChangeTypes = (watcher.WatchingChangeTypes | WatcherChangeTypes.Renamed);
            }
            if ((watch.ChangeTypes & WatcherChangeTypes.Created) == WatcherChangeTypes.Created)
            {
                watcher.Created            += new FileSystemEventHandler(watcher_Created);
                watcher.WatchingChangeTypes = (watcher.WatchingChangeTypes | WatcherChangeTypes.Created);
            }
            if ((watch.ChangeTypes & WatcherChangeTypes.Changed) == WatcherChangeTypes.Changed)
            {
                watcher.Changed            += new FileSystemEventHandler(watcher_Changed);
                watcher.WatchingChangeTypes = (watcher.WatchingChangeTypes | WatcherChangeTypes.Changed);
            }

            int rowIndex = dgrdTaskList.Rows.Add();

            dgrdTaskList.Rows[rowIndex].Cells[IDX_WATCHINGDIR].Value = watcher.Path;
            dgrdTaskList.Rows[rowIndex].Cells[IDX_BACKUPDIR].Value   = watcher.BackupFolder;
            dgrdTaskList.Rows[rowIndex].Cells[IDX_STATUS].Value      = GetWatchingStatusName(watcher.EnableRaisingEvents);
            dgrdTaskList.Rows[rowIndex].Tag = watcher;

            DataGridViewComboBoxCell cell = dgrdTaskList[IDX_CHANGETYPE, rowIndex] as DataGridViewComboBoxCell;

            cell.Items.Add("创建、修改及重命名");
            cell.Items.Add("仅创建");
            cell.Items.Add("仅修改");
            cell.Items.Add("仅重命名");
            if (watch.ChangeTypes == WatcherChangeTypes.Changed)
            {
                cell.Value = "仅修改";
            }
            else if (watch.ChangeTypes == WatcherChangeTypes.Created)
            {
                cell.Value = "仅创建";
            }
            else if (watch.ChangeTypes == WatcherChangeTypes.Renamed)
            {
                cell.Value = "仅重命名";
            }
            else
            {
                cell.Value = "创建、修改及重命名";
            }
            cell.ReadOnly = !watch.Enable;

            DataGridViewCheckBoxCell checkbox = dgrdTaskList.Rows[rowIndex].Cells[IDX_SUBDIR] as DataGridViewCheckBoxCell;

            if (checkbox != null)
            {
                checkbox.Value = watch.IncludeSubdirectories;
            }
            dgrdTaskList.Rows[rowIndex].Cells[IDX_SUBDIR].ReadOnly = !watch.Enable;

            if (watch.Enable)
            {
                watcher.StartWatch();
            }
            else
            {
                watcher.StopWatch();
            }

            //同时将该监视项添加到watcherList中
            watcherList.Add(watcher);
        }