当前位置: 首页>>代码示例>>C#>>正文


C# Directory.GetFiles方法代码示例

本文整理汇总了C#中System.IO.Directory.GetFiles方法的典型用法代码示例。如果您正苦于以下问题:C# Directory.GetFiles方法的具体用法?C# Directory.GetFiles怎么用?C# Directory.GetFiles使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.Directory的用法示例。


在下文中一共展示了Directory.GetFiles方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

// For Directory.GetFiles and Directory.GetDirectories
// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;

public class RecursiveFileProcessor 
{
    public static void Main(string[] args) 
    {
        foreach(string path in args) 
        {
            if(File.Exists(path)) 
            {
                // This path is a file
                ProcessFile(path); 
            }               
            else if(Directory.Exists(path)) 
            {
                // This path is a directory
                ProcessDirectory(path);
            }
            else 
            {
                Console.WriteLine("{0} is not a valid file or directory.", path);
            }        
        }        
    }

    // Process all files in the directory passed in, recurse on any directories 
    // that are found, and process the files they contain.
    public static void ProcessDirectory(string targetDirectory) 
    {
        // Process the list of files found in the directory.
        string [] fileEntries = Directory.GetFiles(targetDirectory);
        foreach(string fileName in fileEntries)
            ProcessFile(fileName);

        // Recurse into subdirectories of this directory.
        string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
        foreach(string subdirectory in subdirectoryEntries)
            ProcessDirectory(subdirectory);
    }
        
    // Insert logic for processing found files here.
    public static void ProcessFile(string path) 
    {
        Console.WriteLine("Processed file '{0}'.", path);	    
    }
}
开发者ID:.NET开发者,项目名称:System.IO,代码行数:50,代码来源:Directory.GetFiles

示例2: Main

//引入命名空间
using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        try 
        {
            // Only get files that begin with the letter "c".
            string[] dirs = Directory.GetFiles(@"c:\", "c*");
            Console.WriteLine("The number of files starting with c is {0}.", dirs.Length);
            foreach (string dir in dirs) 
            {
                Console.WriteLine(dir);
            }
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
开发者ID:.NET开发者,项目名称:System.IO,代码行数:24,代码来源:Directory.GetFiles

示例3: Main

//引入命名空间
using System;
using System.IO;

class MainClass
{
  public static void Main() 
  {
    string[] aFiles = Directory.GetFiles("c:\\");

    foreach (string s in aFiles)
      Console.WriteLine(s);
  }

}
开发者ID:C#程序员,项目名称:System.IO,代码行数:15,代码来源:Directory.GetFiles


注:本文中的System.IO.Directory.GetFiles方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。