using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO; // for File
using System.Management; // for ドライブ情報の取得 (注)参照の追加 要!
namespace sample
{
/// <summary>
/// Form2 の概要の説明です。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.ListView listView1;
private System.ComponentModel.IContainer components = null ;
public Form2()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
}
/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form2));
this.treeView1 = new System.Windows.Forms.TreeView();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.splitter1 = new System.Windows.Forms.Splitter();
this.listView1 = new System.Windows.Forms.ListView();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
this.treeView1.Dock = System.Windows.Forms.DockStyle.Left;
this.treeView1.Font = new System.Drawing.Font("MS UI Gothic", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
this.treeView1.ImageIndex = -1;
this.treeView1.ItemHeight = 16;
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(352, 329);
this.treeView1.Sorted = true;
this.treeView1.TabIndex = 2;
this.treeView1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.treeView1_KeyDown);
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
this.treeView1.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
this.treeView1.Enter += new System.EventHandler(this.treeView1_Enter);
//
// button1
//
this.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button1.BackColor = System.Drawing.SystemColors.Control;
this.button1.Location = new System.Drawing.Point(313, 336);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "選択";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button2.BackColor = System.Drawing.SystemColors.Control;
this.button2.Location = new System.Drawing.Point(185, 336);
this.button2.Name = "button2";
this.button2.TabIndex = 0;
this.button2.Text = "キャンセル";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// imageList1
//
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// splitter1
//
this.splitter1.Location = new System.Drawing.Point(352, 0);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(5, 329);
this.splitter1.TabIndex = 7;
this.splitter1.TabStop = false;
//
// listView1
//
this.listView1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.Location = new System.Drawing.Point(357, 0);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(215, 329);
this.listView1.Sorting = System.Windows.Forms.SortOrder.Ascending;
this.listView1.TabIndex = 3;
this.listView1.View = System.Windows.Forms.View.List;
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(255)), ((System.Byte)(192)));
this.ClientSize = new System.Drawing.Size(572, 369);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.listView1,
this.splitter1,
this.treeView1,
this.button1,
this.button2});
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MinimumSize = new System.Drawing.Size(400, 200);
this.Name = "Form2";
this.ShowInTaskbar = false;
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "フォルダ / ファイル の選択";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);
}
#endregion
public string selected_object = "" ;
private void Form2_Load(object sender, System.EventArgs e) // ← ShowDialog() で起動される
{
// 各ドライブの表示
treeView1.ImageList = imageList1 ;
// Index 0 = フォルダ(非選択)
// 1 = フォルダ(選択)
// 2 = FDD
// 3 = HDD
// 4 = CD-ROM
// 5 = リムーバブル ディスク
string[] drives = Directory.GetLogicalDrives() ;
foreach(string drive in drives)
{
string drive2 = drive.Substring(0, 2) ; // 末尾の'\'をカット
// ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + drive2 + "\"") ;
// disk.Get() ;
// disk["VolumeName"] で ボリューム名 @string がわかる!
// disk["Description"] で ドライブの種類 @string (FDD,HDD,CD) がわかる!
// *** 上記、実行時間が10秒以上かかるので中止!***
TreeNode tn = new TreeNode(drive2) ;
tn.ImageIndex = (drive2 == "A:") ? 2 : 3 ;
tn.SelectedImageIndex = (drive2 == "A:") ? 2 : 3 ;
treeView1.Nodes.Add(tn) ;
tn.Nodes.Add("dummy") ; // +を表示するためにダミーノード追加
}
// ディレクトリ位置の再現
string[] dirs = selected_object.Split(new Char[] {'\\'}) ;
string di2 = "" ;
TreeNodeCollection tnc = treeView1.Nodes ;
foreach(string di in dirs)
{
di2 += di ;
foreach(TreeNode tn in tnc)
{
if (tn.FullPath.ToString() == di2)
{
tn.Expand() ;
treeView1.SelectedNode = tn ;
tnc = tn.Nodes ;
break ;
}
}
di2 += "\\" ;
}
}
private int focused_listview = 0 ;
private void button1_Click(object sender, System.EventArgs e)
{
// 選択 ボタン
if (focused_listview == 1)
selected_object += listView1.SelectedItems[0].Text ;
this.Close() ;
}
private void button2_Click(object sender, System.EventArgs e)
{
// キャンセル ボタン
selected_object = "" ;
this.Close() ;
}
private void treeView1_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
{
// ノードを展開しようとする時の処理(ノード下の情報を作成する)
TreeNode selectedNode = e.Node ;
selectedNode.Nodes.Clear() ; // ダミーノードをクリアするので必要
selected_object = selectedNode.FullPath + "\\" ;
DirectoryInfo selectedDir = new DirectoryInfo(selected_object) ;
if (selectedDir.Exists) // ディレクトリが存在すればノードに追加
{
DirectoryInfo[] subDirInfo = selectedDir.GetDirectories() ;
//Array.Sort(subDirInfo, new compare_DirectoryInfo_name()) ; ←「treeView1.Sorted = true」で代替!
foreach(DirectoryInfo di in subDirInfo)
{
// ユーザーが使えないフォルダは表示しない
//if (di.Attributes == FileAttributes.System) continue ; ← うまく動作しない!
string di2 = di.FullName.Substring(1).ToLower() ;
if ((di2 == ":\\recycled") ||
(di2 == ":\\recycler") ||
(di2 == ":\\system volume information"))
continue ;
try
{
TreeNode nd = selectedNode.Nodes.Add(di.Name) ; // サブディレクトリをノードに追加
nd.ImageIndex = 0 ; // 閉じたフォルダアイコン指定
nd.SelectedImageIndex = 1 ; // 開いたフォルダアイコン指定
DirectoryInfo[] subSubInfo = di.GetDirectories() ;
if(subSubInfo.Length > 0) // 下位にディレクトリがあれば
nd.Nodes.Add("dummy") ; // +を表示するためにダミーノード追加
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation) ;
}
}
}
else
{
MessageBox.Show(
selectedDir.Root.Name.Substring(0, 2) + " にメディアを挿入してください。",
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private TreeNode selected_node ;
private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
// 選択範囲が変更された時の処理
selected_object = e.Node.FullPath + "\\" ;
selected_node = e.Node ;
DirectoryInfo di = new DirectoryInfo(selected_object) ;
listView1.Clear() ; // これがないとファイル表示が累積
if ((e.Node.Text != "A:") && // +マークによるノード展開でデフォルトのA:\アクセス回避
(di.Exists))
{
try
{
foreach (FileInfo file in di.GetFiles())
listView1.Items.Add(file.Name) ;
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation) ;
}
}
if (listView1.Items.Count >= 1)
{
listView1.Items[0].Selected = true ;
listView1.Items[0].Focused = true ; // これがないと、Tab で listView に focus した時、
// Item でなく listView に focus してしまう!
}
}
private void treeView1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
// treeView で Enter キーが押された時、ノードの 展開/折り畳み を行う。
if (e.KeyCode == Keys.Enter)
selected_node.Toggle() ;
}
private void listView1_Enter(object sender, System.EventArgs e)
{
focused_listview = 1 ;
}
private void treeView1_Enter(object sender, System.EventArgs e)
{
focused_listview = 0 ;
}
}
}