文件遍历的二个算法

1.递归算法:
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
using  System.IO;
using  System.Diagnostics;

namespace  TreeViewTest
{
    
public partial class Form1 : Form
    
{
        
//rootPath as property

        
public Form1()
        
{
            InitializeComponent();
        }


        
private void PopulateTreeView(String rootPath)
        
{
            
this.treeView1.Nodes.Clear();

            Stopwatch sw 
= new Stopwatch();
            sw.Start();

            TreeNode rootNode;
            DirectoryInfo dirInfo 
= new DirectoryInfo(rootPath);

            
if (dirInfo.Exists)
            
{
                rootNode 
= new TreeNode(dirInfo.Name,0,1);
                rootNode.Tag 
= dirInfo;
                GetDirectories(dirInfo.GetDirectories(), rootNode);
                treeView1.Nodes.Add(rootNode);
            }


            label1.Text 
= sw.Elapsed.ToString();
        }


        
private void GetDirectories(DirectoryInfo[] subDirInfos,TreeNode subNode)
        
{            
            TreeNode subSubNode;

            
foreach (DirectoryInfo subSubDirInfo in subDirInfos)
            
{
                subSubNode 
= new TreeNode(subSubDirInfo.Name,0,1);
                subSubNode.Tag 
= subSubDirInfo;
                
try
                
{
                    
if (subSubDirInfo.GetDirectories().Length != 0)
                    
{
                        GetDirectories(subSubDirInfo.GetDirectories(), subSubNode);
                    }

                }

                
catch (UnauthorizedAccessException)
                
{
                    
continue;
                }

                getFiles(subSubNode);
                subNode.Nodes.Add(subSubNode);                
            }

            getFiles(subNode);
        }


        
private void getFiles(TreeNode dirNode)
        
{
            TreeNode fileNode;
            DirectoryInfo fileDir 
= (DirectoryInfo)dirNode.Tag;
            
try
            
{
                
foreach (FileInfo finfo in fileDir.GetFiles())
                
{
                    fileNode 
= new TreeNode(finfo.Name, 23);
                    fileNode.Tag 
= finfo;
                    dirNode.Nodes.Add(fileNode);
                }

            }

            
catch (UnauthorizedAccessException)
            
{ }
        }


        
private void button1_Click(object sender, EventArgs e)
        
{
            
if (this.folderBrowserDialog1.ShowDialog() != DialogResult.Cancel)
            
{
                PopulateTreeView(folderBrowserDialog1.SelectedPath);
            }

            
//treeView1.ExpandAll();
        }

    }

}

2.非递归算法:

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Windows.Forms;
using  System.Drawing;
using  System.IO;
using  System.Diagnostics;

namespace  WindowsApplication3
{
    
class DirTreeAdvance : TreeView
    
{
        
private int fileCount;
        
private int directoryCount;
        
private ImageList imageList1;
        
private System.ComponentModel.IContainer components;
        
private TimeSpan ellapsedTime;

        
public DirTreeAdvance()
        
{
            InitializeComponent();
            
this.ImageList = imageList1;
        }


        
public TimeSpan EllapsedTime
        
{
            
get return ellapsedTime; }
        }


        
public int FileCount
        
{
            
get return fileCount; }
        }


        
public int DirectoryCount
        
{
            
get return directoryCount; }
        }


        
public void TraverseFolder(string rootDir)
        
{
            Stopwatch sw 
= new Stopwatch();
            sw.Start();

            
//BeginUpdate();
            this.Nodes.Clear();

            Stack
<TreeNode> directoryNodeStack = new Stack<TreeNode>();

            DirectoryInfo directoryInfo 
= new DirectoryInfo(rootDir);
            TreeNode node 
= new TreeNode(rootDir, 01);
            node.Tag 
= directoryInfo;
            
this.Nodes.Add(node);
            
//push into stack
            directoryNodeStack.Push(node);

            
while (directoryNodeStack.Count > 0)
            
{
                TreeNode currentDirNode 
= directoryNodeStack.Pop();
                directoryInfo 
= currentDirNode.Tag as DirectoryInfo;

                
try
                
{
                    
foreach (FileSystemInfo childFileSystem in directoryInfo.GetFileSystemInfos())
                    
{
                        node 
= new TreeNode(childFileSystem.Name);
                        node.Tag 
= childFileSystem;
                        
if (childFileSystem is FileInfo)
                        
{
                            node.ImageIndex 
= 2;
                            node.SelectedImageIndex 
= 3;
                            fileCount
++;
                        }

                        
else
                        
{
                            node.ImageIndex 
= 0;
                            node.SelectedImageIndex 
= 1;
                            directoryCount
++;
                            
//push into stack
                            directoryNodeStack.Push(node);
                        }

                        currentDirNode.Nodes.Add(node);
                    }

                }

                
catch (UnauthorizedAccessException)
                
{
                    
continue;
                }

            }

            
//EndUpdate();

            ellapsedTime 
= sw.Elapsed;
        }


        
private void InitializeComponent()
        
{
            
this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources 
= new System.ComponentModel.ComponentResourceManager(typeof(DirTreeAdvance));
            
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            
this.SuspendLayout();
            
// 
            
// imageList1
            
// 
            this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
            
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
            
this.imageList1.Images.SetKeyName(0"CLOSED.BMP");
            
this.imageList1.Images.SetKeyName(1"OPEN.BMP");
            
this.imageList1.Images.SetKeyName(2"doc.ico");
            
this.imageList1.Images.SetKeyName(3"folder.ico");
            
this.ResumeLayout(false);

        }

    }

}

你可能感兴趣的:(算法)