[topic] フォルダパス一覧のツリー構造への変換
Posted feedbacks - C#
C#で素直に実装してみました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | using System;
using System.Collections;
using System.Text;
class Sample102
{
private readonly IDictionary dirTree_ = new SortedList();
public Sample102(string[] pathList) {
createTree(pathList);
}
private void createTree(string[] pathList) {
foreach (string path in pathList) {
string[] dirs = path.Split('\\');
addTree(dirs);
}
}
private void addTree(string[] dirs) {
IDictionary prevDict = dirTree_;
for (int index = 0; index < dirs.Length; index++) {
IDictionary dict = prevDict[dirs[index]] as IDictionary;
if (dict == null) {
dict = new SortedList();
prevDict[dirs[index]] = dict;
}
prevDict = dict;
}
}
public void printTree() {
Console.WriteLine(printTree(0, "", dirTree_));
}
private string printTree(int depth, string name, IDictionary innerDir) {
StringBuilder builder = new StringBuilder();
for (int index = 0; index < depth; index++) builder.Append(" ");
builder.Append('/');
builder.Append(name);
builder.Append('\n');
foreach (string dir in innerDir.Keys) {
builder.Append(printTree(depth + 1, dir, innerDir[dir] as IDictionary));
}
return builder.ToString();
}
[STAThread]
static void Main(string[] args) {
string[] pathList = new string[] {
@"abc\def",
@"abc\def\gh",
@"abc\def\ij",
@"abc\jk\lm",
@"de",
};
Sample102 pathTree = new Sample102(pathList);
pathTree.printTree();
Console.ReadLine();
}
}
|
System.Windows.FormsのTreeViewに表示させてみました。
see: 貧脚レーサーのサボり日記
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | //http://ja.doukaku.org/
//http://ja.doukaku.org/102/投稿用
using System;
using System.Windows.Forms;
namespace どう書く_orgフォルダパス一覧のツリー構造への変換 {
class Program {
[STAThread]
static void Main(string[] args) {
Application.Run(new Form1());
}
}
class Form1:Form {
//treeView1
TreeView treeView1 = new TreeView();
//起動時引数でパス一覧のファイルを指定
string pathListFilePath = System.Environment.GetCommandLineArgs()[1];
public Form1() {
//Form1
this.Load += new EventHandler(Form1_Load);
//treeView1
treeView1.Parent = this;
treeView1.Dock = DockStyle.Fill;
}
void Form1_Load(object sender, EventArgs e) {
//ROOTNode
TreeNode rootNode = new TreeNode("ROOT");
treeView1.Nodes.Add(rootNode);
foreach(string fullPath in System.IO.File.ReadAllLines(pathListFilePath)) {
TreeNode addNode = rootNode;
foreach(string path in fullPath.Split(new char[] { '\\' })) {
bool flag = true;
foreach(TreeNode node in addNode.Nodes) {
if(node.Text == path) {
addNode = node;
flag = false;
}
}
if(flag) {
TreeNode newnode = new TreeNode(path);
addNode.Nodes.Add(newnode);
addNode = newnode;
}
}
}
rootNode.ExpandAll();
}
}
}
|



todogzm
#4458()
Rating1/1=1.00
フォルダパスの区切り文字は'\'文字を使用しています。
以下のような1行1パスのフォルダのパスがあった場合、
abc\def
abc\def\gh
abc\def\ij
abc\jk\lm
de
イメージとして、以下のようなツリー構造を構築できればOKです。
ROOT
┗abc
┗def
┗gh
┗ij
┗jk
┗lm
┗de
ツリーですのでルートがあります。上記のフォルダ一覧はルート以下にぶら下げてください。また、同じフォルダにぶら下がっているサブフォルダの名前は重複させてはいけません。
上記のような出力は結果の分かりやすさとしてあった方がいいですが、なければないで構いません。また、ASやJavaFXでグラフィカルな結果を表示するプログラムでも構いませんが、データ構造はちゃんと作ってください。
[ reply ]