ファイルマネージャのファイルとディレクトリの一覧部分をリストビューで作成しました。
ファイル名:FolderManager.cs
// ファイスシステムアイテム
using Microsoft.VisualBasic;
public class FileSystemItem
{
public string FullPath { get; set; }
public string Name { get; set; }
public FileSystemItem() : this(System.Environment.CurrentDirectory)
{
}
public FileSystemItem(string fullpath)
{
this.FullPath = fullpath;
this.Name = Path.GetFileName(fullpath);
}
// ドライブの一覧を取得
public IEnumerable<FileSystemItem> Drives()
{
List<FileSystemItem> result = [];
if (this.FullPath == "|")
{
DriveInfo[] drives = DriveInfo.GetDrives();
foreach(var x in drives)
{
if (x.IsReady)
{
result.Add(new FileSystemItem()
{
Name = x.Name.TrimEnd('\\'),
FullPath = x.Name,
});
}
}
}
return result;
}
// ディレクトリの一覧を取得
public IEnumerable<FileSystemItem> Directoies()
{
List<FileSystemItem> result = [];
if (this.FullPath != "|")
{
var pt = Path.GetDirectoryName(this.FullPath);
if (string.IsNullOrEmpty(pt)) pt = "|";
result.Add(new FileSystemItem()
{
Name = "..",
FullPath = pt,
});
string[] directories = Directory.GetDirectories(this.FullPath);
foreach(var x in directories)
{
var attributes = File.GetAttributes(x);
if ((attributes & (FileAttributes.Hidden | FileAttributes.System)) == 0)
result.Add(new FileSystemItem(x));
}
}
return result;
}
// ファイルの一覧を取得
public IEnumerable<FileSystemItem> Files()
{
List<FileSystemItem> result = [];
if (this.FullPath != "|")
{
string[] files = Directory.GetFiles(this.FullPath);
foreach (var x in files)
{
var attributes = File.GetAttributes(x);
if ((attributes & (FileAttributes.Hidden | FileAttributes.System)) == 0)
result.Add(new FileSystemItem(x));
}
}
return result;
}
}
// ファイルマネージャ
public class FileManger
{
FileSystemItem _currentFolder;
// コンストラクタ
public FileManger(string fullPath="")
{
// カレントディレクトリをセット
if (fullPath != "|" && !Path.Exists(fullPath))
{
fullPath = Environment.CurrentDirectory;
}
_currentFolder = new FileSystemItem(fullPath);
}
// ディレクトリの移動
public bool ChangeDir(string fullPath)
{
bool result = !File.Exists(fullPath);
if (result)
{
_currentFolder.FullPath = fullPath;
}
return result;
}
// ドライブの一覧
public IEnumerable<FileSystemItem> GetDrives() => _currentFolder.Drives();
// ディレクトリの一覧
public IEnumerable<FileSystemItem> GetDirectories() => _currentFolder.Directoies();
// ファイルの一覧
public IEnumerable<FileSystemItem> GetFiles() => _currentFolder.Files();
}
ファイル名:IconManager.cs
public class IconManager
{
static string _systemDir = Environment.GetFolderPath(Environment.SpecialFolder.System);
static string shell32Path = Path.Combine(_systemDir, "shell32.dll");
static public Icon? GetSystemIcon(int index, int size)
{
Icon? result = null;
if (!File.Exists(shell32Path)) return result;
try
{
return Icon.ExtractIcon(shell32Path, index, size);
}
catch
{
return null;
}
}
}
ファイル名:Form1.cs
using System.Diagnostics;
namespace Folder02;
public partial class Form1 : Form
{
FileManger _manger;
ImageList _largeImageList = new()
{
ImageSize = new Size(256, 256),
};
ImageList _smallImageList = new()
{
ImageSize = new Size(24, 24),
};
ListView _listView = new()
{
Dock = DockStyle.Fill,
View = View.Details,
};
// コンストラクタ
public Form1()
{
InitializeComponent();
_manger = new FileManger("|");
_listView.Columns.Add("名前", 360, HorizontalAlignment.Left);
Init_ImageList();
_listView.ItemActivate += ListView_ItemActivate;
this.Controls.Add(_listView);
}
// イメージリストの初期化
void Init_ImageList()
{
// フォルダーアイコン
var folderIcon = IconManager.GetSystemIcon(3, 256);
if (folderIcon is not null)
{
_largeImageList.Images.Add(folderIcon);
_smallImageList.Images.Add(folderIcon);
}
// ファイルアイコン
var fileIcon = IconManager.GetSystemIcon(0, 256);
if (fileIcon is not null)
{
_largeImageList.Images.Add(fileIcon);
_smallImageList.Images.Add(fileIcon);
}
// ドライブアイコン
var DriveIcon = IconManager.GetSystemIcon(7, 256);
if (DriveIcon is not null)
{
_largeImageList.Images.Add(DriveIcon);
_smallImageList.Images.Add(DriveIcon);
}
_listView.LargeImageList = _largeImageList;
_listView.SmallImageList = _smallImageList;
}
// ロード
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
ListView_Update();
}
// リストビューを更新
void ListView_Update()
{
_listView.BeginUpdate();
_listView.Items.Clear();
foreach(var x in _manger.GetDrives())
{
var item = new ListViewItem(x.Name, 2)
{
Tag = x.FullPath,
};
_listView.Items.Add(item);
}
foreach(var x in _manger.GetDirectories())
{
var item = new ListViewItem(x.Name, 0)
{
Tag = x.FullPath,
};
_listView.Items.Add(item);
}
foreach(var x in _manger.GetFiles())
{
var item = new ListViewItem(x.Name, 1)
{
Tag = x.FullPath
};
_listView.Items.Add(item);
}
_listView.EndUpdate();
}
// リストビューアイテムのダブルクリック
void ListView_ItemActivate(object? sender, EventArgs e)
{
if (_listView.SelectedItems.Count <= 0) return;
var tag = _listView.SelectedItems[0].Tag;
if (tag is null) return;
string path = (string)tag;
if (File.Exists(path))
{
// ファイル
Process.Start(new ProcessStartInfo(path) { UseShellExecute = true });
}
else
{
// ディレクトリ
_manger.ChangeDir(path);
ListView_Update();
}
}
}
機能はフォルダアイテムをダブルクリックでカレントディレクトリを移動します。ファイルアイテムをダブクリックすると関連付けされたアプリでファイルを開きます。
コメント