c# Dos Tree

 用法DOS 命令 发现 Tree 命令很有趣 就用C# 写了一个

写得不好请指教  

http://files.cnblogs.com/hcmfys/code.rar 

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


namespace com.qiuSmile
{
    public class FileTree
    {
        /// <summary>
        /// 此程序是模仿DOS tree功能
        /// 作者 相思雨 hcmfys@163.com
        /// 2008-09-14 中秋节
        // 使用方法
        //  FileTree.showTree("c:\\");
        //如果要显示文件  FileTree.showFile=true;
        // 如果要写入文件则加上  FileTree.writeFile("d:\\test.txt");
        /// our knowlege belong to all human
        /// </summary>
        private static int nodeCount = 1;
        private static bool first = false;
        private static DirectoryInfo root = null;
        private static string splitString = "├─";
        private static string empty = "  ";
        private static string insertString = "│";
        private static string fileStr = "";
        public static bool showFile = false;// show file name
        public static void showTree(string dir)
        {
            try
            {
                string str = "";
                StringBuilder strBulider = new StringBuilder();

                DirectoryInfo dirDirctory = new DirectoryInfo(dir);

                if (!first)
                {
                    root = new DirectoryInfo(dir);
                    first = true;
                }
                DirectoryInfo[] sonDir = dirDirctory.GetDirectories();
                int len = sonDir.Length;
                for (int i = 0; i <= len - 1; i++)
                {
                    str = "";

                    nodeCount = getFatherCount(sonDir[i], root);
                    for (int k = 0; k < nodeCount; k++)
                    {
                        str += empty;
                    }
                    str += str + splitString + sonDir[i].Name;

                    if (!sonDir[i].Parent.FullName.Equals(root.FullName))
                    {
                        if (nodeCount != 0)
                        {
                            string tmpStr = "";
                            for (int j = 0; j < nodeCount; j++)
                            {
                                int index = -1;
                                tmpStr += empty;
                                index = tmpStr.Length;
                                if (j == 0) index = 0;
                                if (j > 1) index = index + (j - 1) * 2;
                                str = str.Insert(index, insertString);
                                str = str.Remove(index + insertString.Length, insertString.Length);
                            }
                        }
                    }
                    Console.WriteLine(str);
                    fileStr += str + "\n";
                    showTree(sonDir[i].FullName);
                }
                if (showFile)
                {
                    FileInfo[] fSonDir = dirDirctory.GetFiles();
                    len = fSonDir.Length;
                    for (int i = 0; i <= len - 1; i++)
                    {
                        str = "";

                        nodeCount = getFatherCount(fSonDir[i].Directory, root);
                        for (int k = 0; k < nodeCount; k++)
                        {
                            str += empty;
                        }
                        str += str + splitString +"> "+ fSonDir[i].Name;

                        if (!fSonDir[i].Directory.FullName.Equals(root.FullName))
                        {
                            if (nodeCount != 0)
                            {
                                string tmpStr = "";
                                for (int j = 0; j < nodeCount; j++)
                                {
                                    int index = -1;
                                    tmpStr += empty;
                                    index = tmpStr.Length;
                                    if (j == 0) index = 0;
                                    if (j > 1) index = index + (j - 1) * 2;
                                    str = str.Insert(index, insertString);
                                    str = str.Remove(index + insertString.Length, insertString.Length);
                                }
                            }
                        }

                        Console.WriteLine(str);
                        fileStr += str + "\n";
                    }

                }
            }
            catch (Exception) { }
        }
        /// <summary>
        /// 获取父节点的数目
        /// </summary>
        /// <param name="myInfo"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        private static int getFatherCount(DirectoryInfo myInfo, DirectoryInfo root)
        {
            int i = 0;
            DirectoryInfo father = myInfo.Parent;
            while (father != null && !father.FullName.Equals(root.FullName))
            {
                father = father.Parent;
                i++;
            }
            return i;
        }

        public static void writeFile(string fileName)
        {
            File.WriteAllText(fileName, fileStr);
        }
        /// <summary>
        /// test main method
        /// </summary>
        public static void test()
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.BackgroundColor = ConsoleColor.Black;
            Console.Title = "c# dos Tree";
            Console.WriteLine("请你输入要显示的目录");
            string inputString = Console.ReadLine();
            //FileTree.showFile = true;
            if (inputString.Trim().Length > 0)
            {
                showTree(inputString);
            }
            writeFile("d:\\12.txt");
            Console.WriteLine("按任意键退出.....");
            Console.Read();
        }

    }

}

0
0
(请您对文章做出评价)
« 上一篇:Ie和firefox的Js区别
» 下一篇:ASP.Net中自定义Http处理及应用之HttpModule篇 (转载)
posted @ 2008-09-15 18:31 hcmfys_lover 阅读(235) 评论(1)  编辑 收藏 网摘

  回复  引用    
#1楼2008-10-14 13:39 | weeeeee[未注册用户]
opener即谁打开我的,比如A页面利用window.open弹出了B页面窗口,那么A页面所在窗口就是B页面的

opener,在B页面通过opener对象可以访问A页面。

parent表示父窗口,比如一个A页面利用iframe或frame调用B页面,那么A页面所在窗口就是B页面的

parent。



在JS中,window.opener只是对弹出窗口的母窗口的一个引用。比如:
a.html中,通过点击按钮等方式window.open出一个新的窗口b.html。那么在b.html中,就可以通过

window.opener(省略写为opener)来引用a.html,包括a.html的document等对象,操作a.html的内容。
假如这个引用失败,那么将返回null。所以在调用opener的对象前,要先判断对象是否为null,否则会

出现“对象为空或者不存在”的JS错误。

<html>
<body>
<form. name=form1>
<input type=text name=inpu >
<input type=button >
</form>
</body>
</html>


--------------------------------
back2opener.html
--------------------------------
<html>
<body>
<form. name=form1>
<input type=text name=inpu >

<a class=under href=# >添加</a>
</form>
</body>
</html>



window.opener 返回的是创建当前窗口的那个窗口的引用,比如点击了a.htm上的一个链接而打开了

b.htm,然后我们打算在b.htm上输入一个值然后赋予a.htm上的一个id为“name”的textbox中,就可以

写为:

window.opener.document.getElementById("name").value = "输入的数据";