示例#1
0
        private void FileTreeItemCtxMenu_New_File_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new NewKeyDlg()
            {
                PromptText = "Please enter the file name:"
            };

            if (dlg.ShowDialog() == true)
            {
                var context = FileTree.SelectedItem as FileTreeViewItem;
                if (context.Type == FileTreeViewItemType.FILE)
                {
                    context = context.Parent;
                }
                var path = System.IO.Path.Combine(context.Path, dlg.InputName.Text);
                if (File.Exists(path))
                {
                    MessageBox.Show("File already exists.");
                    return;
                }
                var hFile = CreateFile(path, 0, 0, (IntPtr)0, 1, 0x80, (IntPtr)0);
                CloseHandle(hFile);
                var newItem = new FileTreeViewItem(path)
                {
                    Parent = context
                };
                context.Items.Add(newItem);
            }
        }
示例#2
0
        private void FileTreeItemCtxMenu_New_Folder_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new NewKeyDlg()
            {
                PromptText = "Please enter the folder name:"
            };

            if (dlg.ShowDialog() == true)
            {
                var context = FileTree.SelectedItem as FileTreeViewItem;
                if (context.Type == FileTreeViewItemType.FILE)
                {
                    context = context.Parent;
                }
                var path = System.IO.Path.Combine(context.Path, dlg.InputName.Text);
                CreateDirectory(path, (IntPtr)0);
                var newItem = new FileTreeViewItem(path)
                {
                    Parent = context
                };
                int index = 0;
                for (; index < context.Items.Count; index++)
                {
                    if (context.Items[index].Type == FileTreeViewItemType.FILE)
                    {
                        break;
                    }
                }
                context.Items.Insert(index, newItem);
            }
        }
示例#3
0
        private ProjectItem CreateProjectItem([NotNull] Project project, [NotNull] FileTreeViewItem fileTreeViewItem)
        {
            Debug.ArgumentNotNull(project, nameof(project));
            Debug.ArgumentNotNull(fileTreeViewItem, nameof(fileTreeViewItem));

            var file = fileTreeViewItem.FileUri.FileName;

            if (project.Contains(file))
            {
                return(null);
            }

            var result = new ProjectFileItem(project)
            {
                File = file
            };

            return(result);
        }
        /// <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   = e.FullPath.Length >= 260 ? Path.GetDirectoryName(Kernel32.GetShortPath(e.FullPath)) : e.FullPath;
                var items = this.MainTree.ItemsSource as ObservableCollection <FileTreeViewItem>;
                var item  = items.FirstOrDefault(x => x.Root == dir);
                if (item != null)
                {
                    var newItem = new FileTreeViewItem(dir, this.SearchPattern, this.IsFileEnabled, this.IsIconEnabled)
                    {
                        IsExpanded = item.IsExpanded
                    };
                    var index = items.IndexOf(item);
                    items.RemoveAt(index);
                    items.Insert(index, newItem);
                }
            }), DispatcherPriority.Normal);
        }
        /// <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 ファイルシステムの監視
        }