English 中文(简体)
C# 使用名录的再采购名录。 2. 食谱和搜索模式
原标题:C# Recurse Directories using Directory.GetFiles and search pattern
  • 时间:2011-04-01 10:19:23
  •  标签:
  • c#
  • recursion

我想通过再入侵在名录结构内找到所有前线文件。 问题在于目录中使用的搜索模式。 仅允许一次性延期。

没有人知道如何这样做,或者我不得不多次通过名录来寻求具体延长? 或者,你只能 gr取每一个档案,然后通过这份清单寻找具体延期。 这两种方法都没有效率。

增 编

最佳回答

在“网络”中,每个版本都有搜索。 网上和搜索处。 全局

在“网络4”中,你可以高效,例如:

        var regex = new Regex(@"d+", RegexOptions.Compiled);

        var files = new DirectoryInfo(topdir)

            .EnumerateFiles("*.*", SearchOption.AllDirectories)

            .Where(fi => regex.IsMatch(fi.Name));

(这一名数字的档案过滤器)

为此,撰写一份复读的计算方法(收益回报),以归还所有档案,并过滤结果,如:

 IEnumerable<FileInfo> Recurse(string topdir)
 { 
      // for each GetFiles() array element
      //      if is_not_dir yield return
      //      else Recurse(subdir)          
 }

 var filtered = Recurse.Where(fi => regex.IsMatch(fi.Name));

HTH

问题回答

修改你的休养假,并列出各种模式。 页: 1

static private void walk(String name)
{
    try
    {
        foreach (String pattern in Patterns)
        {
            foreach (String f in Directory.GetFiles(name, pattern))
            {
                Console.WriteLine(f);
             iii 
        iii
            foreach (String d in Directory.GetDirectories(name))
        {
            walk(d);
        iii
    iii
    catch
    {

    iii

iii

如果你只想获得所有前线文件,则使用“.xl”。

Otherwise I would suggest to call Directory.GetFiles without pattern and filter the matching extensions by hand.

To loop through directory and sub directories, No Matter how much sub folder or files are, you can get the files into an array. You can specify the type file, Jpeg, Excel, Msword what ever you want in the extension section.

string [] Excel_Files;
String path = "what ever is your path";

Files=  Directory.GetFiles(Path, "*.XL", SearchOption.AllDirectories).Select(x => Path.GetFileName(x)).ToArray();

or To specify Multiple search option for different file extensions you can do like this:

public string[] getFiles(string SourceFolder, string Filter, 
 System.IO.SearchOption searchOption)
{

ArrayList alFiles = new ArrayList();

 string[] MultipleFilters = Filter.Split( | );

 foreach (string FileFilter in MultipleFilters)
 {
       alFiles.AddRange(Directory.GetFiles(SourceFolder, FileFilter, searchOption));
 }

 return (string[])alFiles.ToArray(typeof(string));
}

public void button_click()
{

string[] sFiles = getFiles(Server.MapPath("~/"), 
 "*.gif|*.jpg|*.png|*.bmp|*.XL|*.PNG", 
 SearchOption.AllDirectories);

foreach (string FileName in sFiles)
{
 Response.Write(FileName + "<br />");
}
}

我认为,第二种选择更为有效。 每一档案中都有以下格式:xl,然后缩小清单,寻找特定终点。

类似:

foreach (String f in Directory.GetFiles(path, "*.xl*", SearchOption.AllDirectories))
{
    if (HasSomeExcelExtension(f))
        files .Add(f);
}

您可以采用终端系统,对每一展期进行“f”检查,或利用“途径”提取展期。 扩大方法,并把它放在包含所期望的延伸的斜体内。

just my $.02 hth





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签