ファイルのコピー、切り取り、貼り付け、キャンセル(コピー、切り取り)、削除機能を追加しました。
ファイル名:Form1.cs
namespace FileManagerControl;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var fm = new FileManager
{
CurrentDirectory = @"H:\text",
Dock = DockStyle.Fill,
};
fm.FileSelected += (s, e) =>
{
var ee = (FileManagerEventArgs)e;
MessageBox.Show(ee.FullPath,"ファイルが選択されたよ。");
};
var cms = new ContextMenuStrip();
var pathViewMenuItem = new ToolStripMenuItem{ Text = "パスを表示", Visible = true };
var deleteMenuItem = new ToolStripMenuItem{ Text = "削除", Visible = true };
var copyMenuItem = new ToolStripMenuItem{ Text = "コピー", Visible = true };
var cutMenuItem = new ToolStripMenuItem{ Text = "切り取り", Visible = true };
var cancelMenuItem = new ToolStripMenuItem{ Text = "キャンセル", Visible = false };
var pasteMenuItem = new ToolStripMenuItem{ Text = "貼り付け", Visible = false };
cms.Items.AddRange(new ToolStripMenuItem[]{
pathViewMenuItem,
deleteMenuItem,
copyMenuItem,
cutMenuItem,
cancelMenuItem,
pasteMenuItem});
fm.ContextMenuStrip = cms;
// パスの表示
var pathViewAction = new Action(() => {
string text = "";
foreach(var t in fm.GetSlectedPath())
{
text = text + t + "\n";
}
MessageBox.Show(text, "パスの表示");
});
pathViewMenuItem.Click += (s, e) => pathViewAction();
// 削除
var deleteAction = new Action(async () => {
var files = new List<string>(fm.GetSlectedPath());
if (!files.Any()) return;
var r = MessageBox.Show(files[0], "削除しますか?", MessageBoxButtons.YesNo, MessageBoxIcon.Hand);
if (r != DialogResult.Yes) return;
var result = await Task.Run(()=>{
int count = 0;
foreach(var path in files)
{
if (File.Exists(path))
{
File.Delete(path);
count++;
}
}
return count;
});
fm.Reload();
});
deleteMenuItem.Click += (s, e) => deleteAction();
// コピー
List<string> copyItems = new();
var copyAction = new Action(()=>{
// 選択項目を配列にセット
foreach(var t in fm.GetSlectedPath())
{
copyItems.Add(t);
}
if (copyItems.Any())
{
copyMenuItem.Visible = false;
cutMenuItem.Visible = false;
cancelMenuItem.Visible = true;
pasteMenuItem.Visible = true;
}
});
copyMenuItem.Click += (s, e) => copyAction();
// 切り取り
List<string> cutItems = new();
var cutAction = new Action(()=>{
// 選択項目を配列にセット
foreach(var t in fm.GetSlectedPath())
{
cutItems.Add(t);
}
if (cutItems.Any())
{
copyMenuItem.Visible = false;
cutMenuItem.Visible = false;
cancelMenuItem.Visible = true;
pasteMenuItem.Visible = true;
}
});
cutMenuItem.Click += (s, e) => cutAction();
// キャンセル
var cancelAction = new Action(()=>{
copyItems.Clear();
cutItems.Clear();
copyMenuItem.Visible = true;
cutMenuItem.Visible = true;
cancelMenuItem.Visible = false;
pasteMenuItem.Visible = false;
});
cancelMenuItem.Click += (s, e) => cancelAction();
// 貼り付け
var pasteAction = new Action(async ()=>{
string dstDir = fm.CurrentDirectory;
var result = await Task.Run(() => {
int count = 0;
// コピー
foreach(var srcPath in copyItems)
{
var dstPath = Path.Join(dstDir, Path.GetFileName(srcPath));
if (File.Exists(srcPath) && !File.Exists(dstPath))
{
// ディレクトリの場合どうしよう?
File.Copy(srcPath, dstPath);
count++;
};
}
// 切り取り
foreach(var srcPath in cutItems)
{
var dstPath = Path.Join(dstDir, Path.GetFileName(srcPath));
if (File.Exists(srcPath) && !File.Exists(dstPath))
{
File.Move(srcPath, dstPath);
count++;
}
}
return count;
});
System.Diagnostics.Debug.Print("{0}", result);
cancelAction();
// 再読み込み
fm.Reload();
});
pasteMenuItem.Click += (s, e) => pasteAction();
// キーイベント
this.KeyPreview = true;
this.KeyDown += (s, e) => {
if (e.KeyData == (Keys.Control | Keys.C)) {
copyAction();
} else if (e.KeyData == (Keys.Control | Keys.X)){
cutAction();
} else if (e.KeyData == (Keys.Control | Keys.V)){
pasteAction();
} else if (e.KeyData == Keys.Delete){
deleteAction();
} else if (e.KeyData == Keys.Escape){
cancelAction();
}
};
// コントロールを追加
Controls.Add(fm);
}
}
コメント