遍历一个目录

作者: 众网友 时间: 2004-2-19

//从csdn.net上看到的,没试过
protected void FolderList1()
{
    DropDownList ddl = this.ddlHead;
    ListItem lt;
    string sValue = "";

    string sHeadDir = Server.MapPath("./..") + "\\XSLTTest"; //文件夹路经
    Response.Write("查看" + sHeadDir + "目录下*.aspx文件:");
    if (System.IO.Directory.Exists(sHeadDir))
    {
        string[] sHead = System.IO.Directory.GetFiles(sHeadDir, "*.aspx");  //查询指定扩展名的文件
        for (int i = 0; i < sHead.Length; i++)
        {
            sValue = sHead[i].ToString();
            sValue = sValue.Substring(sValue.LastIndexOf("\\") + 1, sValue.Length - sValue.LastIndexOf("\\") - 1);
            sValue = sValue.Split('.')[0].ToString();

            lt = new ListItem(sValue, sValue);
            ddl.Items.Add(lt);
        }
    }
}

方法2

查找文件夹sDir下的满足*s*.aspx的文件

protected void FolderList2(string sDir)
{
    try
    {
        foreach (string d in Directory.GetDirectories(sDir))
        {
            foreach (string f in Directory.GetFiles(d, "*s*.aspx"))
            {
                lstFilesFound.Items.Add(f);
            }
            FolderList2(d);
        }
    }
    catch (System.Exception excpt)
    {
        Response.Write(excpt.Message);
    }
}

private TreeNode FillTreeNode(DirectoryInfo dirInfo)
{
    TreeNode node = new TreeNode(dirInfo.Name);
    DirectoryInfo[] dis = dirInfo.GetDirectories();
    foreach (DirectoryInfo di in dis)
    {
        node.Nodes.Add(FillTreeNode(di));
    }
    FileInfo[] curDirFiles = dirInfo.GetFiles();
    foreach (FileInfo fInfo in curDirFiles)
    {
        if (fInfo.Extension == ".zip")
            node.Nodes.Add(fInfo.Name);
    }
    return node;
}

//这个看上去好一些

刚写了一个类似的东东,贴出来了

private void GetDirs(string strDir)
{
    GetDotNetFiles(strDir);
    string[] dirs = Directory.GetDirectories(strDir);
    foreach (string dir in dirs)
    {
        GetDirs(dir);
    }
}

private void GetDotNetFiles(string strDir)
{
    string[] dirs = Directory.GetFiles(strDir, "*.cs");
    foreach (string dir in dirs)
    {
        try
        {
            StreamReader sr = new StreamReader(dir);

            string strContent = sr.ReadToEnd();

            sr.Close();

            FileStream fs = new FileStream(dir, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);

            fs.Position = 0;
            string st = "//************************************************************" + Convert.ToChar(13) + Convert.ToChar(10);
            info = new UTF8Encoding(true).GetBytes(st);
            fs.Write(info, 0, info.Length);

            info = new UTF8Encoding(true).GetBytes(strContent);
            fs.Write(info, 0, info.Length);

            fs.Close();

            this.textBox2.Text = this.textBox2.Text + Convert.ToChar(13) + Convert.ToChar(10) + dir + " add Comments Success";
        }
        catch (Exception ex)
        {
            this.textBox2.Text = this.textBox2.Text + Convert.ToChar(13) + Convert.ToChar(10) + dir + " add Comments Failed: --" + ex.Message;
        }
    }
}

Tester t = new Tester();
string theDirectory = "@c:\WinNT";
DirectoryInfo dir = new DirectoryInfo(theDirectory);

t.ExploreDirectory(dir);

Console.WriteLine("\n\n{0} files in {1}directories found.\n", fileCounter, dirCounter);

private void ExploreDirectory(DirectoryInfo dir)
{
    indentLevel++;
    for (int i = 0; i < indentLevel; i++)
        Console.Write("");
    Console.WriteLine("[0]{1}{2}\n", indentLevel, dir.Name, dir.LastAccessTime);

    FileInfo[] fileInDir = dir.GetFiles();
    foreach (FileInfo file in filesInDir)
    {
        for (int i = 0; i < indentLevel + 1; i++)
            Console.Write("");
        Console.WriteLine("{0}[{1}]Size:{2} bytes",
         file.Name,
         file.LastWriteTime,
         file.Length);
        fileCounte++;
    }
    DirectoryInfo[] directories = dir.GetDirectories();
    foreach (DirectoryInfo newDir in directories)
    {
        dirCounter++;
        ExploreDirectory(newDir);
    }
    indentLevel--
}

static int dirCounter = 1;
static int indentLevel = -1;
static int fileCounter = 0;
}
} 
Contributors: FHL