We do not always find visible happiness in proportion to visible virtue

梦幻白桦林

SHARE

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  14 Posts :: 58 Stories :: 62 Comments :: 0 Trackbacks

公告

常用链接

留言簿(5)

搜索

  •  

最新随笔

最新评论

阅读排行榜

    /// <summary>
    
/// 非递归遍历文件夹
    
/// </summary>
    public class UnRecursionDirectoryReader 
    {
        
public event UnRecursionRead OnUnRecursionItemRead;
        
public event UnRecursionRead OnUnRecursionDequeue;
        
public event UnRecursionRead OnUnRecursionEnqueue;

        
public void Read(string Path)
        {
            
string P_Path = Path;
            Queue
<string> source = new Queue<string>();
            
bool IsHasChild = true;//是否有子文件夹            
            while (IsHasChild)
            {
                
string[] Directorys = System.IO.Directory.GetDirectories(P_Path);
                
string[] Files = System.IO.Directory.GetFiles(P_Path);
                
foreach (string dir in Directorys)
                {                       
                    source.Enqueue(dir); 
//入队列,开始访问文件夹
                    if (OnUnRecursionEnqueue != null) OnUnRecursionEnqueue(dir, P_Path);
                }
                
foreach (string file in Files)
                {                     

                    if (OnUnRecursionItemRead != null) OnUnRecursionItemRead(file, P_Path); //访问文件
                }
                
if (source.Count > 0)
                {
                    
string OldPath = P_Path;
                    P_Path 
= source.Dequeue();//出队列,更换文件夹
                    if (OnUnRecursionDequeue != null) OnUnRecursionDequeue(P_Path, OldPath);
                }
                
else
                {
                    IsHasChild 
= false;
                }
            }
        }
    }

demo:
         
TreeNode TargetNode = null//指向treeview的结点
    Queue<TreeNode> TreeQueue = new Queue<TreeNode>(); //临时存放

        
private void read()
    {
           CYS.UnRecursionDirectoryReader TreeReader 
= new CYS.UnRecursionDirectoryReader();
            TreeReader.OnUnRecursionDequeue
+=new CYS.UnRecursionRead(TreeReader_OnUnRecursionDequeue);
            TreeReader.OnUnRecursionEnqueue
+=new CYS.UnRecursionRead(TreeReader_OnUnRecursionEnqueue);
            TreeReader.OnUnRecursionItemRead
+=new CYS.UnRecursionRead(TreeReader_OnUnRecursionItemRead);
            TreeReader.Read(AppRoot);            
        }

        
private void TreeReader_OnUnRecursionItemRead(string DireName, string Parent)
        {            
            TargetNode.Nodes.Add(System.IO.Path.GetFileName(DireName));
        }

        
private void TreeReader_OnUnRecursionDequeue(string DireName, string Parent)
        {
            TargetNode 
= TreeQueue.Dequeue();
        }

        
private void TreeReader_OnUnRecursionEnqueue(string DireName, string Parent)
        {
            TreeNode Node 
= new TreeNode(System.IO.Path.GetFileName(DireName));
            TargetNode.Nodes.Add(Node);
            TreeQueue.Enqueue(Node);
        }




posted on 2007-09-18 09:39 colys 阅读(1313) 评论(0)  编辑 收藏 引用 所属分类: C#.Net

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理