/// <summary> /// ファイルシステム監視イベントハンドラ /// </summary> /// <param name="sender">イベント発行元</param> /// <param name="e">イベント引数</param> private void OnFileSystemUpdated(object sender, FileSystemEventArgs e) { if (_dispatcher == null) return; _dispatcher.BeginInvoke(new Action(() => { var dir = Path.GetDirectoryName(e.FullPath); var items = this.MainTree.ItemsSource as ObservableCollection<FileTreeViewItem>; var item = items.FirstOrDefault(x => x.FullPath == dir); if (item != null) { var newItem = new FileTreeViewItem(dir, this.SearchPattern, this.IsFileEnabled) { IsExpanded = item.IsExpanded }; var index = items.IndexOf(item); items.RemoveAt(index); items.Insert(index, newItem); } System.Diagnostics.Trace.WriteLine("ファイルシステムが更新されましたぁ!"); }), DispatcherPriority.Normal); }
/// <summary> /// 初期化をおこないます。 /// </summary> private void Initilization() { // テンプレート適用前は処理しない if (this.MainTree == null) return; var w = Window.GetWindow(this); if (w == null) return; ObservableCollection<FileTreeViewItem> rootCollection = null; if (Directory.Exists(this.RootPath)) { var root = new FileTreeViewItem(this.RootPath, this.SearchPattern, this.IsFileEnabled) { IsExpanded = true }; rootCollection = new ObservableCollection<FileTreeViewItem>() { root, }; } else { var handle = (new WindowInteropHelper(w)).Handle; #region マイコンピュータ _myComputer = new FileTreeViewItem("", this.SearchPattern, this.IsFileEnabled); _myComputer.Name = "マイコンピュータ"; _myComputer.IsExpanded = true; _myComputer.IsExpanded = false; _myComputer.BitmapByteArray = Shell32.ShellInfo.GetSpecialIconByByteArray(handle, Shell32.ShellInfo.FolderID.MyComputer); _myComputer.Children = new ObservableCollection<FileTreeViewItem>(); var infoArray = DriveInfo.GetDrives(); foreach (var info in infoArray) { if (info.IsReady) { (_myComputer.Children as ObservableCollection<FileTreeViewItem>).Add(new FileTreeViewItem(info.RootDirectory.FullName, this.SearchPattern, this.IsFileEnabled)); } } #endregion マイコンピュータ #region マイドキュメント var myDocumentPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); var myDocument = new FileTreeViewItem(myDocumentPath, this.SearchPattern, this.IsFileEnabled); #endregion マイドキュメント #region デスクトップ var desktopPath = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); var desktop = new FileTreeViewItem(desktopPath, this.SearchPattern, this.IsFileEnabled); desktop.Name = "デスクトップ"; desktop.IsExpanded = true; (desktop.Children as Collection<FileTreeViewItem>).Insert(0, _myComputer); (desktop.Children as Collection<FileTreeViewItem>).Insert(1, myDocument); #endregion デスクトップ rootCollection = new ObservableCollection<FileTreeViewItem>() { desktop, }; } this.MainTree.ItemsSource = rootCollection ?? new ObservableCollection<FileTreeViewItem>(); #region ファイルシステムの監視 if (this._watchers != null) { foreach (var watcher in this._watchers) { watcher.EnableRaisingEvents = false; watcher.Changed -= OnFileSystemUpdated; watcher.Created -= OnFileSystemUpdated; watcher.Deleted -= OnFileSystemUpdated; watcher.Renamed -= OnFileSystemUpdated; watcher.Dispose(); } this._watchers.Clear(); } else { this._watchers = new List<FileSystemWatcher>(); } if (this.IsSynchronizeFileSystem) { _dispatcher = w.Dispatcher; foreach (var item in rootCollection) { if (!string.IsNullOrWhiteSpace(item.FullPath) && Directory.Exists(item.FullPath)) { var watcher = new FileSystemWatcher() { Path = item.FullPath, Filter = "", NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite, IncludeSubdirectories = true, SynchronizingObject = w as System.ComponentModel.ISynchronizeInvoke, }; watcher.Changed += OnFileSystemUpdated; watcher.Created += OnFileSystemUpdated; watcher.Deleted += OnFileSystemUpdated; watcher.Renamed += OnFileSystemUpdated; watcher.EnableRaisingEvents = true; this._watchers.Add(watcher); } } } else { this._watchers = null; _dispatcher = null; } #endregion ファイルシステムの監視 }