/// <summary> /// 初期化をおこないます。 /// </summary> private void Initilization() { // テンプレート適用前は処理しない if (this.MainTree == null) { return; } if (this.IsLoaded == false) { return; } var w = Window.GetWindow(this); if (w == null) { return; } // 更新の必要性確認 if (this._isDirty == false) { return; } this._isDirty = false; // とりあえず全クリア this.MainTree.ItemsSource = null; this._rootCollection.Clear(); // ノード確定 if (Directory.Exists(this.RootPath)) { var root = new FileTreeViewItem(this.RootPath, this.SearchPattern, this.IsFileEnabled, this.IsIconEnabled); root.ForceExpanding(); this._rootCollection.Add(root); } else { var handle = (new WindowInteropHelper(w)).Handle; #region マイコンピュータ this._myComputer = new FileTreeViewItem("", this.SearchPattern, this.IsFileEnabled, this.IsIconEnabled); this._myComputer.Name = "マイコンピュータ"; this._myComputer.ForceExpanding(); this._myComputer.BitmapByteArray = Shell32.ShellInfo.GetSpecialIconByByteArray(handle, Shell32.ShellInfo.FolderID.MyComputer); this._myComputer.Children = new ObservableCollection <FileTreeViewItem>(); var infoArray = DriveInfo.GetDrives(); foreach (var info in infoArray) { if (info.IsReady) { this._myComputer.Children.Add(new FileTreeViewItem(info.RootDirectory.FullName, this.SearchPattern, this.IsFileEnabled, this.IsIconEnabled)); } } #endregion マイコンピュータ #region マイドキュメント var myDocumentPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); var myDocument = new FileTreeViewItem(myDocumentPath, this.SearchPattern, this.IsFileEnabled, this.IsIconEnabled); #endregion マイドキュメント #region デスクトップ var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); var desktop = new FileTreeViewItem(desktopPath, this.SearchPattern, this.IsFileEnabled, this.IsIconEnabled); desktop.Name = "デスクトップ"; desktop.ForceExpanding(); desktop.Children.Insert(0, this._myComputer); desktop.Children.Insert(1, myDocument); #endregion デスクトップ this._rootCollection.Add(desktop); } // ツリーのルート要素確定 this.MainTree.ItemsSource = this._rootCollection; #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) { foreach (var item in this._rootCollection) { if (!string.IsNullOrWhiteSpace(item.Root) && Directory.Exists(item.Root)) { var watcher = new FileSystemWatcher() { Path = item.Root, 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); } } } #endregion ファイルシステムの監視 }