查找特定文件路径下指定文件类型的文件

ContractedBlock.gif ExpandedBlockStart.gif 代码
  1None.gifusing System;
  2None.gifusing System.IO;
  3None.gifusing System.Runtime.Remoting.Messaging;
  4None.gifusing System.Collections.Specialized;
  5None.gif
  6None.gifpublic class FindFiles
  7ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  8InBlock.gif    delegate string[] GetFilesHandler(
  9InBlock.gif         string searchPattern, bool recurseSubdirectories);
 10InBlock.gif
 11InBlock.gif    public static void Main(string[] args)
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 13InBlock.gif        bool recurseSubdirectories = true;
 14InBlock.gif        IAsyncResult[] result = new IAsyncResult[args.Length];
 15InBlock.gif        int count = 0;
 16InBlock.gif
 17InBlock.gif        foreach (string arg in args)
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 19InBlock.gif            if (arg.Trim().ToUpper() == "/S")
 20ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 21InBlock.gif                recurseSubdirectories = true;
 22InBlock.gif                break;
 23ExpandedSubBlockEnd.gif            }

 24ExpandedSubBlockEnd.gif        }

 25InBlock.gif
 26InBlock.gif        GetFilesHandler asyncGetFilesHandler = GetFiles;
 27InBlock.gif
 28InBlock.gif        Console.WriteLine("Searching: {0}",
 29InBlock.gif            string.Join("", args));
 30InBlock.gif        if (recurseSubdirectories)
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 32InBlock.gif            Console.WriteLine("\trecursivedot.gif");
 33ExpandedSubBlockEnd.gif        }

 34InBlock.gif        Console.WriteLine("Push ENTER to cancel/exitdot.gif");
 35InBlock.gif
 36InBlock.gif        foreach (string arg in args)
 37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 38InBlock.gif            if (arg.Trim().ToUpper() != "/S")
 39ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 40InBlock.gif                result[count] = asyncGetFilesHandler.BeginInvoke(
 41InBlock.gif                    arg, recurseSubdirectories,
 42InBlock.gif                    SearchCompleted, arg);
 43ExpandedSubBlockEnd.gif            }

 44InBlock.gif            count++;
 45ExpandedSubBlockEnd.gif        }

 46InBlock.gif        Console.ReadLine();
 47ExpandedSubBlockEnd.gif    }

 48InBlock.gif
 49InBlock.gif    public static string[] GetFiles(
 50InBlock.gif       string searchPattern, bool recurseSubdirectories)
 51ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 52InBlock.gif        string[] results = null;
 53InBlock.gif
 54InBlock.gif        // Search for files matching the pattern.
 55InBlock.gif        StringCollection files = new StringCollection();
 56InBlock.gif        string directory;
 57InBlock.gif        directory = Path.GetDirectoryName(searchPattern);
 58InBlock.gif        if ((directory == null|| (directory.Trim().Length == 0))
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 60InBlock.gif
 61InBlock.gif            directory = Directory.GetCurrentDirectory();
 62ExpandedSubBlockEnd.gif        }

 63InBlock.gif
 64InBlock.gif        files.AddRange(GetFiles(searchPattern));
 65InBlock.gif
 66InBlock.gif        if (recurseSubdirectories)
 67ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 68InBlock.gif            foreach (string subDirectory in
 69InBlock.gif                 Directory.GetDirectories(directory))
 70ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 71InBlock.gif                files.AddRange(GetFiles(
 72InBlock.gif                    Path.Combine(
 73InBlock.gif                        subDirectory,
 74InBlock.gif                        Path.GetFileName(searchPattern)),
 75InBlock.gif                    true));
 76ExpandedSubBlockEnd.gif            }

 77ExpandedSubBlockEnd.gif        }

 78InBlock.gif
 79InBlock.gif        results = new string[files.Count];
 80InBlock.gif        files.CopyTo(results, 0);
 81InBlock.gif
 82InBlock.gif        return results;
 83ExpandedSubBlockEnd.gif    }

 84InBlock.gif
 85InBlock.gif    public static string[] GetFiles(string searchPattern)
 86ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 87InBlock.gif        string[] fileNames;
 88InBlock.gif        string directory;
 89InBlock.gif
 90InBlock.gif        // Set directory , default to the current if the
 91InBlock.gif        // is none specified in the searchPattern.
 92InBlock.gif        directory = Path.GetDirectoryName(searchPattern);
 93InBlock.gif        if ((directory == null|| (directory.Trim().Length == 0))
 94ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 95InBlock.gif            directory = Directory.GetCurrentDirectory();
 96ExpandedSubBlockEnd.gif        }

 97InBlock.gif
 98InBlock.gif        fileNames = Directory.GetFiles(
 99InBlock.gif                Path.GetFullPath(directory),
100InBlock.gif                Path.GetFileName(searchPattern));
101InBlock.gif
102InBlock.gif        return fileNames;
103ExpandedSubBlockEnd.gif    }

104InBlock.gif
105InBlock.gif    public static void SearchCompleted(IAsyncResult result)
106ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
107InBlock.gif        string searchPattern = (string)result.AsyncState;
108InBlock.gif        Console.WriteLine("{0}:", searchPattern);
109InBlock.gif        AsyncResult asyncResult = (AsyncResult)result;
110InBlock.gif        GetFilesHandler handler =
111InBlock.gif                (GetFilesHandler)asyncResult.AsyncDelegate;
112InBlock.gif        string[] files = handler.EndInvoke(result);
113InBlock.gif        foreach (string file in files)
114ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
115InBlock.gif            Console.WriteLine("\t" + Path.GetFileName(file));
116ExpandedSubBlockEnd.gif        }

117ExpandedSubBlockEnd.gif    }

118ExpandedBlockEnd.gif}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值