CodeSmith模板引擎系列二--文件目录树

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

Template Code:

 

ExpandedBlockStart.gif 代码
<% @ 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
内容比较少今天就到这里。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值