ITeye博客

单线程 文件查找

hcmfys · 2010-02-06 17:14 · c# · 编程语言 · C · C++ · C# · Markdown

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ThreadTest
{
    public class SingleFileSearch
    {
        private long c = 0;
        public void Search(string dir, string fileName)
        {

            string[] fileList = Directory.GetFiles(dir);
            foreach (string file in fileList)
            {
                int startIndex = file.LastIndexOf('\\');
                int endIndex = file.LastIndexOf('.');
                if (endIndex > startIndex)
                {
                    if (file.Substring(startIndex + 1, endIndex - startIndex).IndexOf(fileName) >= 0)
                        c++;
                }
            }
            try
            {
                string[] dirList = Directory.GetDirectories(dir);
                foreach (string _dir in dirList)
                {
                    Search(_dir, fileName);
                }
            }
            catch (Exception) { }

        }
        public void Complete()
        {
            Console.WriteLine("完成操作 " + c);
        }

    }
}


DateTime t1 = DateTime.Now;
            FileSearch file = new FileSearch(64, "d:/", "c");
            file.Run();
            file.Complete();
            DateTime t2 = DateTime.Now;
            TimeSpan s = t2 - t1;
            Console.WriteLine("_________________" + s.TotalSeconds);

 

            t1 = DateTime.Now;
            SingleFileSearch sinFile = new SingleFileSearch();
            sinFile.Search("d:/", "c");
            sinFile.Complete();
            t2 = DateTime.Now;
            s = t2 - t1;
            Console.WriteLine("_________________" + s.TotalSeconds);

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。