| 1 | /*
|
|---|
| 2 | * dirtree.cs
|
|---|
| 3 | * $Id$
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright (C) 2005 Mayuki Sawatari, All rights reserved.
|
|---|
| 6 | *
|
|---|
| 7 | * THIS SOFTWARE IS PROVIDED BY THE MISUZILLA.ORG ``AS IS'' AND
|
|---|
| 8 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|---|
| 9 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|---|
| 10 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|---|
| 11 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|---|
| 12 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|---|
| 13 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|---|
| 14 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|---|
| 15 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|---|
| 16 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|---|
| 17 | * SUCH DAMAGE.
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 | using System;
|
|---|
| 21 | using System.IO;
|
|---|
| 22 | using System.Collections;
|
|---|
| 23 | using System.Drawing;
|
|---|
| 24 | using System.Drawing.Text;
|
|---|
| 25 | using System.Drawing.Imaging;
|
|---|
| 26 | using System.Drawing.Drawing2D;
|
|---|
| 27 | using System.Windows.Forms;
|
|---|
| 28 |
|
|---|
| 29 | public class DirTree {
|
|---|
| 30 | public static Int32 Main(String[] args)
|
|---|
| 31 | {
|
|---|
| 32 | String dirPath;
|
|---|
| 33 |
|
|---|
| 34 | if (args.Length == 0 || !Directory.Exists(args[0])) {
|
|---|
| 35 | FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
|
|---|
| 36 | folderBrowserDialog.Description = "�ΏۂƂȂ��H���_���Ă��������B";
|
|---|
| 37 | if (folderBrowserDialog.ShowDialog() == DialogResult.OK) {
|
|---|
| 38 | dirPath = folderBrowserDialog.SelectedPath;
|
|---|
| 39 | } else {
|
|---|
| 40 | return 1;
|
|---|
| 41 | }
|
|---|
| 42 | } else {
|
|---|
| 43 | dirPath = args[0];
|
|---|
| 44 | }
|
|---|
| 45 | Console.Write("Directory[{0}]", dirPath);
|
|---|
| 46 | Node node = GetDirectoryAndFileNodes(dirPath, 0);
|
|---|
| 47 | Console.WriteLine("");
|
|---|
| 48 |
|
|---|
| 49 | Console.Write("Writing dir tree");
|
|---|
| 50 | Bitmap bmp = new Bitmap(400, (node.Height+1) * 25);
|
|---|
| 51 | _stringFormat.LineAlignment = StringAlignment.Center;
|
|---|
| 52 | _stringFormat.Alignment = StringAlignment.Center;
|
|---|
| 53 | _penGray.DashStyle = DashStyle.Dot;
|
|---|
| 54 | using (Graphics g = Graphics.FromImage(bmp)) {
|
|---|
| 55 | g.Clear(Color.White);
|
|---|
| 56 | g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
|
|---|
| 57 | DrawTree(g, node, 0);
|
|---|
| 58 | }
|
|---|
| 59 | Console.WriteLine("");
|
|---|
| 60 |
|
|---|
| 61 | // �ۑ�
|
|---|
| 62 | Console.WriteLine("Write to file.");
|
|---|
| 63 | SaveFileDialog saveFileDialog = new SaveFileDialog();
|
|---|
| 64 | saveFileDialog.Filter = "Portable Network Graphics �t�@�C�� (*.png)|*.png";
|
|---|
| 65 | saveFileDialog.RestoreDirectory = true;
|
|---|
| 66 |
|
|---|
| 67 | if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
|---|
| 68 | using (Stream stream = saveFileDialog.OpenFile()) {
|
|---|
| 69 | if (stream != null)
|
|---|
| 70 | bmp.Save(stream, ImageFormat.Png);
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | return 0;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | private static Pen _penBlack = new Pen(Color.Black, 1);
|
|---|
| 78 | private static Pen _penGray = new Pen(Color.Gray, 1);
|
|---|
| 79 | private static Pen _penWhite = new Pen(Color.White, 1);
|
|---|
| 80 | private static Font _drawFont = new Font("Tahoma", 8, FontStyle.Regular);
|
|---|
| 81 | private static SolidBrush _drawBrush = new SolidBrush(Color.Black);
|
|---|
| 82 | private static SolidBrush _drawBrushGray = new SolidBrush(Color.Gray);
|
|---|
| 83 | private static SolidBrush _drawBrushLightGray = new SolidBrush(Color.LightGray);
|
|---|
| 84 | private static SolidBrush _drawBrushWhite = new SolidBrush(Color.White);
|
|---|
| 85 | private static StringFormat _stringFormat = new StringFormat();
|
|---|
| 86 |
|
|---|
| 87 | public static void DrawTree(Graphics g, Node node, Int32 top)
|
|---|
| 88 | {
|
|---|
| 89 | Console.Write(".");
|
|---|
| 90 | g.FillRectangle(_drawBrushLightGray,
|
|---|
| 91 | new Rectangle(
|
|---|
| 92 | 20 * node.Depth, 25 * top, 130, 15
|
|---|
| 93 | )
|
|---|
| 94 | );
|
|---|
| 95 | g.DrawRectangle(_penGray,
|
|---|
| 96 | new Rectangle(
|
|---|
| 97 | 20 * node.Depth, 25 * top, 130, 15
|
|---|
| 98 | )
|
|---|
| 99 | );
|
|---|
| 100 | g.DrawString(node.Name,
|
|---|
| 101 | _drawFont, _drawBrushGray,
|
|---|
| 102 | new RectangleF(
|
|---|
| 103 | 20 * node.Depth, 25 * top, 130, 15
|
|---|
| 104 | ),
|
|---|
| 105 | _stringFormat
|
|---|
| 106 | );
|
|---|
| 107 |
|
|---|
| 108 | Int32 prevCornerPointTop = top * 25 + 15;
|
|---|
| 109 | foreach (Node c in node.Children) {
|
|---|
| 110 | top++;
|
|---|
| 111 | // ��g.DrawLine(_penGray,
|
|---|
| 112 | 20 * c.Depth - 10, prevCornerPointTop,
|
|---|
| 113 | 20 * c.Depth - 10, top * 25 + 7.5F
|
|---|
| 114 | );
|
|---|
| 115 | g.DrawLine(_penGray,
|
|---|
| 116 | 20 * c.Depth - 10, top * 25 + 7.5F,
|
|---|
| 117 | 20 * c.Depth, top * 25 + 7.5F
|
|---|
| 118 | );
|
|---|
| 119 |
|
|---|
| 120 | // �g
|
|---|
| 121 | if (c.IsFolder) {
|
|---|
| 122 | // �f�B���N�g��
|
|---|
| 123 | DrawTree(g, c, top);
|
|---|
| 124 | top += c.Height;
|
|---|
| 125 | } else {
|
|---|
| 126 | // �t�@�C��
|
|---|
| 127 | g.DrawRectangle(_penBlack,
|
|---|
| 128 | new Rectangle(
|
|---|
| 129 | 20 * c.Depth, 25 * top, 130, 15
|
|---|
| 130 | )
|
|---|
| 131 | );
|
|---|
| 132 | g.DrawString(c.Name,
|
|---|
| 133 | _drawFont, _drawBrush,
|
|---|
| 134 | new RectangleF(
|
|---|
| 135 | 20 * c.Depth, 25 * top, 130, 15
|
|---|
| 136 | ),
|
|---|
| 137 | _stringFormat
|
|---|
| 138 | );
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | public static Node GetDirectoryAndFileNodes(String startDir, Int32 depth)
|
|---|
| 144 | {
|
|---|
| 145 | Node node = new Node();
|
|---|
| 146 | node.Name = startDir;
|
|---|
| 147 |
|
|---|
| 148 | foreach (String filename in Directory.GetFiles(startDir)) {
|
|---|
| 149 | Console.Write(".");
|
|---|
| 150 | //Console.WriteLine("".PadLeft(depth, ' ')+"-"+Path.GetFileName(filename));
|
|---|
| 151 | Node fileNode = new Node();
|
|---|
| 152 | fileNode.Name = Path.GetFileName(filename);
|
|---|
| 153 | fileNode.Parent = node;
|
|---|
| 154 | node.Children.Add(fileNode);
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | foreach (String dirname in Directory.GetDirectories(startDir)) {
|
|---|
| 158 | Console.Write(".");
|
|---|
| 159 | //Console.WriteLine("".PadLeft(depth, ' ')+"@"+Path.GetFileName(dirname));
|
|---|
| 160 | Node childNode = GetDirectoryAndFileNodes(dirname, depth + 1);
|
|---|
| 161 | childNode.Name = Path.GetFileName(dirname);
|
|---|
| 162 | childNode.Parent = node;
|
|---|
| 163 | node.Children.Add(childNode);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | return node;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | public class Node
|
|---|
| 170 | {
|
|---|
| 171 | public String Name;
|
|---|
| 172 | public Node Parent;
|
|---|
| 173 | private ArrayList _children;
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 | public Node()
|
|---|
| 177 | {
|
|---|
| 178 | _children = new ArrayList();
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | public ArrayList Children
|
|---|
| 182 | {
|
|---|
| 183 | get {
|
|---|
| 184 | return _children;
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 | public Int32 Height
|
|---|
| 188 | {
|
|---|
| 189 | get {
|
|---|
| 190 | Int32 height = 0;
|
|---|
| 191 | foreach (Node node in Children) {
|
|---|
| 192 | height += node.Height + 1;
|
|---|
| 193 | }
|
|---|
| 194 | return height;
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | public Int32 Depth
|
|---|
| 199 | {
|
|---|
| 200 | get {
|
|---|
| 201 | return (Parent == null ? 0 : Parent.Depth + 1);
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | public Boolean IsFolder
|
|---|
| 206 | {
|
|---|
| 207 | get {
|
|---|
| 208 | return (Children.Count > 0);
|
|---|
| 209 | }
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | public String Path
|
|---|
| 213 | {
|
|---|
| 214 | get {
|
|---|
| 215 | if (Parent == null)
|
|---|
| 216 | return Name;
|
|---|
| 217 |
|
|---|
| 218 | return System.IO.Path.Combine(Parent.Path, Name);
|
|---|
| 219 | }
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 | } |
|---|