/*这是我刚学C#的时候写的,用了递归,感觉速度不够快,后来提问好的算法,没人回应。
http://www.csdn.net/expert/topic/378/378586.shtm
我在我的硬盘上查*.bmp,“搜索”用了20秒,这个程序用了26秒,希望会好的算法贴出来。*/using System;
using System.IO;class mainclass
{
static void Main()
{
Console.WriteLine("请输入文件夹路径:");
string path=Console.ReadLine();
find.findFD(path);
}
}class find
{
public static void findFD(string path)
{
try//防止找不到路径而出错
{
string[] files=Directory.GetFiles(path,"*.txt");//找出路经为变量path,*.*表示所有的文件
string[] dirs=Directory.GetDirectories(path,"*.*");//找出路经为变量path,*.*表示所有的目录
foreach (string file in files)
{
Console.WriteLine(file);//列举所有文件
}
foreach (string dir in dirs)//列举所有目录
{
//Console.WriteLine(dir);
if (dirs.Length!=0) //如果目录以下有目录,用递归
{
find.findFD(dir);
}
}
}
catch
{
Console.WriteLine("文件夹不存在或输入错误");
}
}
}

解决方案 »

  1.   

    感谢使用微软产品采用递归查询的方式会影响程序代码的效率,您可以采用.NET Framework中提供的Collection.Queue来取消递归。具体的代码如下:using System;using System.IO;using System.Collections;public static void BeginSearch(string rootpath)                {                        Queue queue_dir = new Queue();                        queue_dir.Enqueue(rootpath);                                                try                        {                                while(true)                                {                                        string temppath = (string)queue_dir.Dequeue();                                        string[] files = Directory.GetFiles(temppath, "*.txt");                                        foreach(string file in files)                                        {                                                System.Console.WriteLine(file);                                        }                                        string[] dirs = Directory.GetDirectories(temppath);                                        foreach(string dir in dirs)                                        {                                                queue_dir.Enqueue(dir);                                        }                                }                        }                        catch                        {                                //目录错误,或者是队列为空,返回                                return;                        }                }======================
    - 微软全球技术中心VC技术支持本贴子仅供CSDN的用户作为参考信息使用。其内容不具备任何法律保障。您需要考虑到并承担使用此信息可能带来的风险。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
    为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
    ======================
      

  2.   

    to 微软全球技术中心VC技术支持:
    看了你的代码,心想你的代码一定比较快了,于是就试一下。
    我在我的硬盘上查*.bmp,“系统搜索”用了20秒,我这个程序用了26秒,你的用了33秒??是我的测试方法有问题还是你的代码要改善?
    希望其他人帮忙比较一下!!!!!
      

  3.   

    感谢使用微软产品。对于读取硬盘文件,系统会有Cache进行缓冲。
    如果您先用了第一个程序,再用第二个程序,由于系统有了缓冲,速度会明显不同的。
    以下是速度的测试数据:(以我们的程序Release版测试)
    第一遍运行:942865157 (tickcount)
    第二遍运行:372939980 (tickcount)
    第三遍运行:81617360 (tickcount)
    以上是对一个程序运行三次的结果。
    所以仅仅采用时间的方式来计算速度是不对的,我们所提供的代码只是取消了函数的
    递归调用,从理论上加快了速度,实际的测试要保证两个程序环境完全一致才能进行。======================
    - 微软全球技术中心VC技术支持本贴子仅供CSDN的用户作为参考信息使用。其内容不具备任何法律保障。您需要考虑到并承担使用此信息可能带来的风险。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
    为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
    ======================