using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { FolderBrowserDialog dlg = new FolderBrowserDialog(); if (dlg.ShowDialog() != DialogResult.OK) { return; } textBox1.Text = dlg.SelectedPath; listBox1.Items.Clear(); Stack> stack = new Stack>(); stack.Push(new Tuple(dlg.SelectedPath, 0)); while (stack.Count > 0) { var temp = stack.Pop(); var dir = temp.Item1; var rank = temp.Item2; listBox1.Items.Add( new String(' ', rank * 2) + Path.GetFileName(dir) + "[フォルダ]"); var subfs = Directory.GetFiles(dir, "*"); foreach (var f in subfs) { listBox1.Items.Add( new String(' ', (rank + 1) * 2) + Path.GetFileName(f) + "[ファイル]"); } if (subfs.Length > 0) { listBox1.Items.Add(""); } var subds = Directory.GetDirectories(dir, "*"); foreach (var d in subds) { stack.Push(new Tuple(d, rank + 1)); } } } } }