注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

悠游自在

似水流年,悠游天地,自在我心

 
 
 

日志

 
 
 
 

目录树转HTML  

2016-07-08 16:38:39|  分类: IT技术 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
最近在工作中,需要分析整理一台服务器上的目录,用tree命令导出该目录,居然有10M+,服务器兄,您辛苦了!
但问题来了,导出的目录文件如何展示和查看,用txt文件和excel文件看起来都太大了,浏览起来太累。所以做了一个小功能,将其转为html,通过html tree支持展开-收起的功能,方便像文件浏览器一样浏览导出的目录结构。主要代码如下所示:
namespace DirTree2HTML
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //读入配置文件,初始化“选择目录”、“保存文件”输入框
            txtDir.Text = System.Configuration.ConfigurationManager.AppSettings["选择目录"];
            txtSave2HtmlFile.Text = System.Configuration.ConfigurationManager.AppSettings["保存文件"];
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            //写入配置文件,保存“选择目录”、“保存文件”配置
            AddUpdateAppSettings("选择目录", txtDir.Text);
            AddUpdateAppSettings("保存文件", txtSave2HtmlFile.Text);
        }
        static void AddUpdateAppSettings(string key, string value)
        {
            try
            {
                var configFile = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
                var settings = configFile.AppSettings.Settings;
                if (settings[key] == null)
                {
                    settings.Add(key, value);
                }
                else
                {
                    settings[key].Value = value;
                }
                configFile.Save(System.Configuration.ConfigurationSaveMode.Modified);
                System.Configuration.ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
            }
            catch (ConfigurationErrorsException)
            {
                Console.WriteLine("Error writing app settings");
            }
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            Dir2Tree.ShowMsgDelegate frmInvokeMsg = ShowProcessMessage;
            Cursor = Cursors.WaitCursor;
            Dir2Tree.ConvertDir2HtmlFile(txtDir.Text, txtSave2HtmlFile.Text, frmInvokeMsg);
        }

        private void ShowProcessMessage(string msg,bool bDone)
        {
            if (textMessage.InvokeRequired)
            {
                Dir2Tree.ShowMsgDelegate frmInvokeMsg = ShowProcessMessage;
                textMessage.Invoke(frmInvokeMsg, msg,bDone);
            }
            else
            {
                if (bDone)
                {
                    MessageBox.Show("转换完成");
                    Cursor = Cursors.Default;
                }
                else
                {
                    textMessage.Text = msg;
                }
            }
        }


        private void btnBrowseDir_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            txtDir.Text = folderBrowserDialog1.SelectedPath;
        }

        private void btnBrowseSave2HtmlFile_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            txtSave2HtmlFile.Text = saveFileDialog1.FileName;
        }

    }
    public class TreeTemplate
    {
        public string FileHead,FileTail,NodeHead,NodeTail,SpanHead,SpanTail,ChildHead,ChildTail,HitArea;

        string _NodeHead, _ImageNode, _ShapeHead, _ChildHead;
        static int uid=1,childid=1;
       
        public TreeTemplate()
        {
            FileHead = File.ReadAllText("TreeTemplate.FileHead.txt");
            FileTail = File.ReadAllText("TreeTemplate.FileTail.txt");

            HitArea = "            <div class=\"d_hitarea d_tree_node_hitarea\"></div>\r\n";

            ChildHead = "            <ul>\r\n";
            ChildTail = "            </ul>\r\n";

            NodeHead = "                <li class=\"d_tree_node d_open d_collapsable\">\r\n";
            NodeTail = "                </li>";

            SpanHead = "                    <span class=\"d_folder\">";
            SpanTail = "</span>\r\n";

        }

    }
    public class Dir2Tree
    {
        static TreeTemplate m_TreeTemplate;
        static StreamWriter m_HtmlFile;
        static StringBuilder m_HtmlStrBuf;
        static bool bInitOK = false;
        static string m_strDir, m_strAxureFile;
        public static string m_strProcessMsg;
        static ShowMsgDelegate m_invokeMsg;

        static void Init()
        {
            //初始化变量
            m_TreeTemplate = new TreeTemplate();
        }

        static void DoConvert()
        {
            string tmpString;
            //打开输出文件
            m_HtmlFile = new StreamWriter(m_strAxureFile);
            m_HtmlStrBuf = new StringBuilder();

            //生成文件头
            tmpString = m_TreeTemplate.FileHead;
            m_HtmlFile.Write(tmpString);
            m_HtmlStrBuf.Append(tmpString);

            //处理一级目录
            string strCurDirNode;
            int nLevel = 0;
            strCurDirNode = m_strDir;
            Covert1LevelDir(strCurDirNode, nLevel);

            //生成文件尾
            tmpString = m_TreeTemplate.FileTail;
            m_HtmlFile.Write(tmpString);
            m_HtmlStrBuf.Append(tmpString);
            //关闭输出文件;
            m_HtmlFile.Close();
            m_invokeMsg("转换完成", true);
        }

        public static void ConvertDir2HtmlFile(string strDir, string strAxureFile, ShowMsgDelegate invokeMsg)
        {
            if (!bInitOK)
            {
                bInitOK = true;
                Init();
            }

            m_strDir = strDir;
            m_strAxureFile = strAxureFile;
            m_invokeMsg=invokeMsg;


            Thread WorkingThread = new Thread(new ThreadStart(DoConvert));
            WorkingThread.Start();

        }
        public delegate void ShowMsgDelegate(string msg,bool bDone);      
       
        private static void Covert1LevelDir(string strCurDirNode, int nLevel)
        {
            DirectoryInfo DI = new DirectoryInfo(strCurDirNode);
            DirectoryInfo[] subDir = DI.GetDirectories();
            string tmpString;
            //遍历当前目录
            //生成当前节点的div 头
            tmpString = m_TreeTemplate.NodeHead;
            m_HtmlFile.Write(tmpString);
            m_HtmlStrBuf.Append(tmpString);

            //如果下面有目录,需要HitArea
            if (subDir.Length > 0)
            {
                tmpString = m_TreeTemplate.HitArea;
                m_HtmlFile.Write(tmpString);
                m_HtmlStrBuf.Append(tmpString);
            }

            //生成shapenode
            tmpString = m_TreeTemplate.SpanHead;
            m_HtmlFile.Write(tmpString);
            m_HtmlStrBuf.Append(tmpString);
            //写入当前目录名
            tmpString = DI.Name;
            m_HtmlFile.Write(tmpString);
            m_HtmlStrBuf.Append(tmpString);
            m_invokeMsg(tmpString,false);
            //封闭目录名span
            tmpString = m_TreeTemplate.SpanTail;
            m_HtmlFile.Write(tmpString);
            m_HtmlStrBuf.Append(tmpString);

            // 子目录不空,则优先遍历所有的子目录,再遍历每个子目录中的文件
            if (subDir.Length > 0)
            {
                tmpString = m_TreeTemplate.ChildHead;
                m_HtmlFile.Write(tmpString);
                m_HtmlStrBuf.Append(tmpString);
                foreach (DirectoryInfo sub in subDir)
                {
                    Covert1LevelDir(sub.FullName, nLevel + 1); // 遍历当前目录的子目录
                }
                tmpString = m_TreeTemplate.ChildTail;
                m_HtmlFile.Write(tmpString);
                m_HtmlStrBuf.Append(tmpString);
            }

            //生成当前节点的div 尾
            tmpString = m_TreeTemplate.NodeTail;
            m_HtmlFile.Write(tmpString);
            m_HtmlStrBuf.Append(tmpString);
        }
    }
}
  评论这张
 
阅读(24)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018