zoukankan      html  css  js  c++  java
  • CodeSmith模板引擎系列二文件目录树

       今天网上Google了一下,关于CodeSmith的资料已经很全了,其中有TerryLee努力学习的小熊 两位大牛的很详尽,所以我也不准备把CodeSmith讲的很细致了,如果是新手学习者可以参考上面两位大牛的CodeSmith文章,CodeSmith的语法与ASP语法接近,语言可以选择我们的c#,所以学习起来很轻松。今天就写一个简单的文件目录树结构的递归模板。

    Template Code:

    代码
    <%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" 
       Description
    ="Template description here." %>
    <%@ Assembly Name="System.Data" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Assembly Name="System" %>
    <%@ Import Namespace="System.IO" %> 

    -----------------------------------------------------------------
     
    -- Date Created: <%= DateTime.Now.ToLongDateString() %>
     
    -- Createdy:   B Generated by Wolf
     
    -----------------------------------------------------------------
     
    <script runat="template">
            
    private string path;
            [Editor(
    typeof(System.Windows.Forms.Design.FolderNameEditor),

             
    typeof(System.Drawing.Design.UITypeEditor))] 
            
    public string Path
            {
                
    get{return this.path;}    
                
    set{this.path=value;}
            }
            
    public void GetFolderString(string fload,string indexStr)
            {
                
    if(string.IsNullOrEmpty(fload))
                    
    throw new System.ArgumentNullException("fload");
                Response.WriteLine(indexStr
    +fload);
                indexStr
    +="----";            
                
    if(System.IO.Directory.Exists(fload))
                {
                    
    string[] childfolder= System.IO.Directory.GetDirectories(fload);
                    
    string[] childfile= System.IO.Directory.GetFiles(fload);
                    
    foreach(string item in childfolder)
                    {
                        
    this.GetFolderString(item,indexStr);
                    }
                    
    foreach(string item in childfile)
                    {
                        
    this.GetFolderString(item,indexStr);
                    }
                }
            }
            
    public void Start()
            {
                
    this.GetFolderString(this.path,"");
            }
    </script>
    <% this.Start(); %>
    模板生成代码比较简单,就不在这里多讲述了,只是拿来展示一下CodeSmith模板对于生成基于Ascii语言文件的强大威力:
    image
    内容比较少今天就到这里。


    作者:破  狼
    出处:http://www.cnblogs.com/whitewolf/
    本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该文章也同时发布在我的独立博客中-个人独立博客博客园--破狼51CTO--破狼

  • 相关阅读:
    Leetcode Unique Binary Search Trees
    Leetcode Decode Ways
    Leetcode Range Sum Query 2D
    Leetcode Range Sum Query
    Leetcode Swap Nodes in Pairs
    Leetcode Rotate Image
    Leetcode Game of Life
    Leetcode Set Matrix Zeroes
    Leetcode Linked List Cycle II
    CF1321A
  • 原文地址:https://www.cnblogs.com/whitewolf/p/1777088.html
Copyright © 2011-2022 走看看