/* * dirtree.cs * $Id$ * * Copyright (C) 2005 Mayuki Sawatari, All rights reserved. * * THIS SOFTWARE IS PROVIDED BY THE MISUZILLA.ORG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ using System; using System.IO; using System.Collections; using System.Drawing; using System.Drawing.Text; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.Windows.Forms; public class DirTree { public static Int32 Main(String[] args) { String dirPath; if (args.Length == 0 || !Directory.Exists(args[0])) { FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog(); folderBrowserDialog.Description = "対象となるフォルダを選択してください。"; if (folderBrowserDialog.ShowDialog() == DialogResult.OK) { dirPath = folderBrowserDialog.SelectedPath; } else { return 1; } } else { dirPath = args[0]; } Console.Write("Directory[{0}]", dirPath); Node node = GetDirectoryAndFileNodes(dirPath, 0); Console.WriteLine(""); Console.Write("Writing dir tree"); Bitmap bmp = new Bitmap(400, (node.Height+1) * 25); _stringFormat.LineAlignment = StringAlignment.Center; _stringFormat.Alignment = StringAlignment.Center; _penGray.DashStyle = DashStyle.Dot; using (Graphics g = Graphics.FromImage(bmp)) { g.Clear(Color.White); g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; DrawTree(g, node, 0); } Console.WriteLine(""); // 保存 Console.WriteLine("Write to file."); SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Portable Network Graphics ファイル (*.png)|*.png"; saveFileDialog.RestoreDirectory = true; if (saveFileDialog.ShowDialog() == DialogResult.OK) { using (Stream stream = saveFileDialog.OpenFile()) { if (stream != null) bmp.Save(stream, ImageFormat.Png); } } return 0; } private static Pen _penBlack = new Pen(Color.Black, 1); private static Pen _penGray = new Pen(Color.Gray, 1); private static Pen _penWhite = new Pen(Color.White, 1); private static Font _drawFont = new Font("Tahoma", 8, FontStyle.Regular); private static SolidBrush _drawBrush = new SolidBrush(Color.Black); private static SolidBrush _drawBrushGray = new SolidBrush(Color.Gray); private static SolidBrush _drawBrushLightGray = new SolidBrush(Color.LightGray); private static SolidBrush _drawBrushWhite = new SolidBrush(Color.White); private static StringFormat _stringFormat = new StringFormat(); public static void DrawTree(Graphics g, Node node, Int32 top) { Console.Write("."); g.FillRectangle(_drawBrushLightGray, new Rectangle( 20 * node.Depth, 25 * top, 130, 15 ) ); g.DrawRectangle(_penGray, new Rectangle( 20 * node.Depth, 25 * top, 130, 15 ) ); g.DrawString(node.Name, _drawFont, _drawBrushGray, new RectangleF( 20 * node.Depth, 25 * top, 130, 15 ), _stringFormat ); Int32 prevCornerPointTop = top * 25 + 15; foreach (Node c in node.Children) { top++; // 線 g.DrawLine(_penGray, 20 * c.Depth - 10, prevCornerPointTop, 20 * c.Depth - 10, top * 25 + 7.5F ); g.DrawLine(_penGray, 20 * c.Depth - 10, top * 25 + 7.5F, 20 * c.Depth, top * 25 + 7.5F ); // 枠 if (c.IsFolder) { // ディレクトリ DrawTree(g, c, top); top += c.Height; } else { // ファイル g.DrawRectangle(_penBlack, new Rectangle( 20 * c.Depth, 25 * top, 130, 15 ) ); g.DrawString(c.Name, _drawFont, _drawBrush, new RectangleF( 20 * c.Depth, 25 * top, 130, 15 ), _stringFormat ); } } } public static Node GetDirectoryAndFileNodes(String startDir, Int32 depth) { Node node = new Node(); node.Name = startDir; foreach (String filename in Directory.GetFiles(startDir)) { Console.Write("."); //Console.WriteLine("".PadLeft(depth, ' ')+"-"+Path.GetFileName(filename)); Node fileNode = new Node(); fileNode.Name = Path.GetFileName(filename); fileNode.Parent = node; node.Children.Add(fileNode); } foreach (String dirname in Directory.GetDirectories(startDir)) { Console.Write("."); //Console.WriteLine("".PadLeft(depth, ' ')+"@"+Path.GetFileName(dirname)); Node childNode = GetDirectoryAndFileNodes(dirname, depth + 1); childNode.Name = Path.GetFileName(dirname); childNode.Parent = node; node.Children.Add(childNode); } return node; } public class Node { public String Name; public Node Parent; private ArrayList _children; public Node() { _children = new ArrayList(); } public ArrayList Children { get { return _children; } } public Int32 Height { get { Int32 height = 0; foreach (Node node in Children) { height += node.Height + 1; } return height; } } public Int32 Depth { get { return (Parent == null ? 0 : Parent.Depth + 1); } } public Boolean IsFolder { get { return (Children.Count > 0); } } public String Path { get { if (Parent == null) return Name; return System.IO.Path.Combine(Parent.Path, Name); } } } }