/// <summary> /// ファイル/フォルダーのリネームを確定させます。 /// </summary> /// <param name="e">呼び出し元のイベント引数</param> private void applyRename(NodeLabelEditEventArgs e, FileTreeNodeEventArgs eArgs, string typeTag, string oldFile, string newFile) { if (!e.CancelEdit) { try { if (typeTag == TagFile) { File.Move(oldFile, newFile); } else { Directory.Move(oldFile, newFile); } eArgs.SelectedNode.Text = Path.GetFileName(eArgs.rPath); //ここで新しいノード名を確定させる } catch (UnauthorizedAccessException) { //アクセス権限がロックされていた場合 e.CancelEdit = true; eArgs.Failed = true; MessageBox.Show(Resources.MsgE_AcccessError, Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); } catch { e.CancelEdit = true; eArgs.Failed = true; MessageBox.Show(Resources.MsgE_Failed.Replace("$", "ファイルの操作"), Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); } } this.RenamedNode?.Invoke(this, eArgs); //失敗しても発生させる }
/// <summary> /// 現在のノードの下にフォルダーを新規作成します。 /// ファイルが選択されている場合は同一フォルダー内に作成します。 /// 実際の作成処理はラベル編集後に行います。 /// </summary> public void CreateFolder() { if (this.raiseSelfNodeChange()) { return; //操作がキャンセルされた } //ファイルを選択している場合は同一フォルダー内に作成する if (this.IsFileSelected) { this.trvTree.SelectedNode = this.trvTree.SelectedNode.Parent; } //名前を決めさせる var Dlg = new Dialog.Common.dlgInputTextValue(this.SelectedNodeFullPath, true, false, "新しいフォルダー"); if (Dlg.ShowDialog(this) == DialogResult.OK) { //フォルダーを生成する var newNode = new TreeNode { Text = Dlg.ResultText }; this.trvTree.SelectedNode.Nodes.Add(newNode); var eArgs = new FileTreeNodeEventArgs { IsFolder = true, rPath = Dlg.rPath, SelectedNode = newNode }; this.applyCreate(eArgs); } }
/// <summary> /// 現在選択されているファイルまたはフォルダーを削除します。 /// </summary> /// <param name="editorCloseMethod">関連エディターを閉じるメソッドへの参照</param> /// <returns>成功したかどうか</returns> public bool DeleteSelectedNode(Action editorCloseMethod) { if (!this.IsSelected || !this.CanDelete) { return(false); } var fileName = Path.GetFileName(this.SelectedNodeFullPath); var BeforeSelectedNode = this.trvTree.SelectedNode; //除外設定 if (this.trvTree.SelectedNode.Parent == null) //ルートフォルダーは削除できない { MessageBox.Show(Resources.MsgE_NGDeleteRoot.Replace("$", "削除"), Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } else if (fileName.Substring(0, 1) == Resources.NG_SystemFolderSymbol) { MessageBox.Show(Resources.MsgE_NGDeleteRoot.Replace("$", "削除"), Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } //発生させるイベントの準備 var eArgs = new FileTreeNodeEventArgs { rPath = this.SelectedNodeFullPath, SelectedNode = this.trvTree.SelectedNode, IsFolder = this.IsFolderSelected }; if (MessageBox.Show(Resources.MsgQ_Confirm.Replace("$", fileName + " を削除"), Resources.AppName, MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { //関連エディターを閉じる処理 editorCloseMethod?.Invoke(); try { //削除を実行 if (this.IsFolderSelected) { Common.DeleteDirectory(Common.AddToPathSplit(Path.GetDirectoryName(Common.CutLastChar(this.RootPath, '\\'))) + this.GetFolder().FullPath); } else { File.Delete(Common.AddToPathSplit(Path.GetDirectoryName(Common.CutLastChar(this.RootPath, '\\'))) + Common.AddToPathSplit(this.GetFolder().FullPath) + fileName); } BeforeSelectedNode.Remove(); } catch { MessageBox.Show(Resources.MsgE_Failed.Replace("$", "削除操作"), Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } else { return(false); } this.DeletedNode?.Invoke(this, eArgs); return(true); }
/// <summary> /// 選択ノードの名前を変更した後 /// </summary> private void trvTree_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { if (e.Node == null) { return; //入力中にリロードが起こるなどして処理できない状態になった } var eArgs = new FileTreeNodeEventArgs(); var oldFile = Common.AddToPathSplit(Path.GetDirectoryName(Common.CutLastChar(this.RootPath, '\\'))) + e.Node?.FullPath; var newFile = Common.AddToPathSplit(Path.GetDirectoryName(Common.CutLastChar(this.RootPath, '\\'))) + Common.AddToPathSplit(Path.GetDirectoryName(e.Node?.FullPath)) + e.Label; var isFolder = (e.Node.Tag?.ToString() == TagFolder || e.Node.Tag?.ToString() == TagTempFolder); //除外設定 if (e.Label == null) { //変更しなかった場合はサイレントスルー e.CancelEdit = true; } else if (!Common.CheckEnabledPath(e.Label)) { //ファイル・フォルダー名に使えない文字が含まれる MessageBox.Show(Resources.MsgE_FileNameInvarid.Replace("$", "ファイル/フォルダー"), Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); e.CancelEdit = true; } else if (File.Exists(newFile)) { //ファイル名と重複しているフォルダー名 MessageBox.Show(Resources.MsgE_FileExists.Replace("$", "ファイル"), Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); e.CancelEdit = true; } else if (Directory.Exists(newFile)) { //フォルダー名と重複しているファイル名 MessageBox.Show(Resources.MsgE_FileExists.Replace("$", "フォルダー"), Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); e.CancelEdit = true; } else if (!isFolder && this.FileFilter != "" && !Common.CheckFileExtensionFilter(newFile, this.FileFilter)) { //該当拡張子が付いていないと、作成してもリストに表示されないので無効にする MessageBox.Show("無効な拡張子です。", Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); e.CancelEdit = true; } //発生させるイベントのパラメーター設定 eArgs.rPath = Path.GetDirectoryName(e.Node.FullPath) + this.trvTree.PathSeparator + e.Label; eArgs.SelectedNode = e.Node; eArgs.IsFolder = isFolder; //リネーム実行 this.applyRename(e, eArgs, e.Node.Tag?.ToString(), oldFile, newFile); }
/// <summary> /// ファイル/フォルダー作成を確定させます。 /// </summary> /// <param name="e">呼び出し元のイベント引数</param> private void applyCreate(FileTreeNodeEventArgs eArgs) { //空のファイル・フォルダーを生成する try { if (!eArgs.IsFolder) { File.WriteAllText(ProjectManager.ProjectPath + eArgs.rPath, "", Common.SJIS); } else { Directory.CreateDirectory(ProjectManager.ProjectPath + eArgs.rPath); } } catch { MessageBox.Show(Resources.MsgE_Failed.Replace("$", "ファイルの操作"), Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); return; //失敗した場合は中断する } eArgs.SelectedNode.Text = Path.GetFileName(eArgs.rPath); //ここで新しいノード名を確定させる this.CreatedNode?.Invoke(this, eArgs); this.ReloadTree(eArgs.SelectedNode.FullPath); }