---
title: "单线程 文件查找"
author: "hcmfys"
date: "2010-02-06T17:14:35Z"
tags: ["C", "C#", "C++"]
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-02-06T17:14:35Z"
id: 588913
url: "https://hcmfys.iteye.com/blog/588913"
---

```
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);