開発環境 Microsoft Visual C# 2010 Express (SP1)
実行環境 Microsoft Windows XP Home Edition (SP3)
プロジェクトの種類 Windows フォーム アプリケーション
プロジェクト名 FileMan

Form Form1
Size:800, 600 Text:FileMan

メニューとツール バー/ToolStrip toolStrip1
toolStripTextBox1

コンポーネント/ImageList imageList1
ColorDepth:Depth32Bit

コンテナー/SplitContainer splitContainer1
FixedPanel:Panel1

Panel1
コモン コントロール/TreeView treeView1
Dock:Fill ImageList:imageList1

Panel2
コモン コントロール/ListView listView1
Dock:Fill SmallImageList:imageList1

参考

Form1.cs
using System;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
 
namespace FileMan
{
    public partial class Form1 : Form
    {
        [DllImport("Shell32.dll", CharSet = CharSet.Auto)]
        extern static uint ExtractIconEx(string lpszFile, int nIconIndex,
            IntPtr[] phiconLarge, IntPtr[] phiconSmall, uint nIcons);
 
        bool ImageAddIcon(string dllPath, ImageList orderImageList)
        {
            uint iconCnt = ExtractIconEx(dllPath, -1, null, null, 1);
            if (iconCnt == 0) return false;
 
            IntPtr[] hIcon = new IntPtr[iconCnt];
            uint getCnt;
            if (orderImageList.ImageSize.Width <= SystemInformation.SmallIconSize.Width &&
                orderImageList.ImageSize.Height <= SystemInformation.SmallIconSize.Height)
            {
                getCnt = ExtractIconEx(dllPath, 0, null, hIcon, iconCnt);
            }
            else
            {
                getCnt = ExtractIconEx(dllPath, 0, hIcon, null, iconCnt);
            }
            if (getCnt < 1) return false;
 
            for (int idx = 0; idx < getCnt; idx++)
            {
                if (hIcon[idx] != IntPtr.Zero)
                {
                    using (Icon icon = Icon.FromHandle(hIcon[idx]))
                    {
                        orderImageList.Images.Add(icon);
                    }
                }
            }
            return true;
        }
 
        public Form1()
        {
            InitializeComponent();
 
            // 画像リストにアイコンを追加
            ImageAddIcon(@"C:\WINDOWS\system32\SHELL32.dll", imageList1);
 
            // リストビューの設定
            listView1.View = View.Details;
            listView1.FullRowSelect = true;
            listView1.GridLines = true;
 
            listView1.Columns.Add("名前", 300);
            listView1.Columns.Add("サイズ", 80, HorizontalAlignment.Right);
            listView1.Columns.Add("更新日時", 120);
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            // ドライブ一覧
            string[] drives = Environment.GetLogicalDrives();
            foreach (string drv in drives)
            {
                string path = drv.TrimEnd('\\');
                DriveInfo di = new DriveInfo(path);
                int index;
                switch (di.DriveType)
                {
                    case DriveType.Removable:
                        index = 6; break;
                    case DriveType.Fixed:
                        index = 7; break;
                    case DriveType.Network:
                        index = 9; break;
                    case DriveType.CDRom:
                        index = 11; break;
                    case DriveType.Ram:
                        index = 12; break;
                    default: continue;
                }
                TreeNode node = new TreeNode(path, index, index);
                treeView1.Nodes.Add(node);
                node.Nodes.Add("");
            }
        }
 
        private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            TreeNode treeNode = e.Node;
            string path = treeNode.FullPath + '\\';
            treeNode.Nodes.Clear();
            DirectoryInfo dirInfo = new DirectoryInfo(path);
            try
            {
                foreach (DirectoryInfo di in dirInfo.GetDirectories())
                {
                    TreeNode node = new TreeNode(di.Name, 3, 4);
                    treeNode.Nodes.Add(node);
                    DirectoryInfo subDir = new DirectoryInfo(di.FullName);
                    if (subDir.GetDirectories().Length > 0)
                    {
                        node.Nodes.Add("");
                    }
                }
            }
            catch { }
        }
 
        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode treeNode = e.Node;
            string path = treeNode.FullPath + '\\';
            toolStripTextBox1.Text = path;
            listView1.Items.Clear();
            DirectoryInfo dirInfo = new DirectoryInfo(path);
            try
            {
                string[] line = new string[3];
                foreach (DirectoryInfo di in dirInfo.GetDirectories())
                {
                    line[0] = di.Name;
                    line[2] = di.CreationTime.ToString(@"yyyy/MM/dd\ HH:mm:ss");
                    ListViewItem item = new ListViewItem(line, 3);
                    listView1.Items.Add(item);
                }
                foreach (FileInfo fi in dirInfo.GetFiles())
                {
                    line[0] = fi.Name;
                    line[1] = fi.Length.ToString("N0");
                    line[2] = fi.LastWriteTime.ToString(@"yyyy/MM/dd\ HH:mm:ss");
                    ListViewItem item = new ListViewItem(line, 70);
                    listView1.Items.Add(item);
                }
            }
            catch { }
        }
    }
}
 
最終更新:2012年12月04日 22:54