English 中文(简体)
收集所有档案和目录的方便选择方法
原标题:Beginner-friendly method to get list of all files and directories
  • 时间:2009-10-10 03:39:15
  •  标签:

采用3。0网,我采用了以下方法,正确收回特定目录的所有档案和目录(和分局)。 如果可能的话,我只想使用我非常舒适的建筑。 具体来说,我不清楚的是:

1。 IEnumerable<FileSystemInfo>: I d like to return List<FileSystemInfo> instead
2。 Stack<FileSystemInfo>: I d list to use List<FileSystemInfo> instead。
3。 yield return: I ve never used this before

public static IEnumerable<FileSystemInfo> GetAllFilesAndDirectories ( string dir ) {

    DirectoryInfo dirInfo = new DirectoryInfo( dir );
    Stack<FileSystemInfo> stack = new Stack<FileSystemInfo>();

    stack。Push( dirInfo );
    while ( dirInfo != null || stack。Count > 0 ) {
        FileSystemInfo fileSystemInfo = stack。Pop();
        DirectoryInfo subDirectoryInfo = fileSystemInfo as DirectoryInfo;
        if ( subDirectoryInfo != null ) {
            yield return subDirectoryInfo;
            foreach ( FileSystemInfo fsi in subDirectoryInfo。GetFileSystemInfos() ) {
                stack。Push( fsi );
            }
            dirInfo = subDirectoryInfo;
        } else {
            yield return fileSystemInfo;
            dirInfo = null;
        }
    }

}

The argument could be made that I should just get comfortable with the code above, but that s not what I m shooting for today。

预 收

最佳回答

我认为,你重新研究现有的“GetFileSystemInfos”(检测、搜查)方法。 如果你把所有目录指定为搜索工具的价值,则会重新检索已经通过的文件。

例:

public static List<FileSystemInfo> GetAllFilesAndDirectories ( string dir ) {
  DirectoryInfo info = new DirectoryInfo(dir);
  FileSystemInfo[] all = info.GetFileSystemInfos("*", SearchOptions.AllDirectories);
  return new List<FileSystemInfo>(all);
}

如果你想写出很长的一段话,你可以做以下工作:

public static List<FileSystemInfo> GetAllFilesAndDirectories ( string dir ) {
  int i = 0; 
  List<DirectoryInfo> toProcess = new List<DirectoryInfo>();
  List<FileSystemInfo> list = new List<FileSystemInfo>();
  toProcess.Add(new DirectoryInfo(dir));
  while ( i < toProcess.Count ) { 
    DirectoryInfo curDir = toProcess[i];
    foreach ( FileSystemInfo curFile in curDir.GetFileSystemInfos() ) {
      list.Add(curFile);
      DirectoryInfo maybe = curFile as DirectoryInfo;
      if ( maybe != null ) {
        toProcess.Add(maybe);
      }
    i++;
  }
  return list;
}

  FileSystemInfo[] all = info.GetFileSystemInfos("*", SearchOptions.AllDirectories);
  return new List<FileSystemInfo>(all);
}
问题回答

这里,我想到的最短路是:

static List<FileSystemInfo> GetAllFilesAndDirectories(string dir)
{
    DirectoryInfo dirInfo = new DirectoryInfo(dir);            
    List<FileSystemInfo> allFilesAndDirectories = new List<FileSystemInfo>();

    allFilesAndDirectories.AddRange(dirInfo.GetFiles("*", SearchOption.AllDirectories));
    allFilesAndDirectories.AddRange(dirInfo.GetDirectories("*", SearchOption.AllDirectories));

    return allFilesAndDirectories;
}

它将从特定道路开始,归还所有各级档案和目录清单。 归还这些档案的命令是所有档案,然后是所有目录。

    public List<Object> GetFilesAndDirectories(string path)
    {
        List<Object> lst = new List<Object>();
        string[] dirs = null;

        try
        {
            dirs = Directory.GetDirectories(path);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        foreach (string d in dirs)
        {
            string[] files = null;

            try
            {
                files = Directory.GetFiles(d);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            foreach (string f in files)
            {
                lst.Add(f);
            }

            lst.Add(d);

            lst.AddRange(GetFilesAndDirectories(d));
        }

        return lst;
    }


List<Object> stuff = GetFilesAndDirectories(someRoot);

你们特别要求“不要这样做”。 我认为,另外两个答案是非常好的,但这里又是以一种更基本、更容易理解的方式,在最初的法典中这样做的另一种方式。

http://support.microsoft.com/kb/303974

<><>Edit>/strong>

我知道这并不是3.0,但它仍在尝试、测试和容易理解这样做的方式。

我认为,从法典的可读性观点来看,你的最佳点是写上一种复读功能。 休养职能是自称的,直至达到不需要指定任何其他职能的地步。

举例来说,N的系数,如N! ,被定义为1×2×3×......x ...... x n(如果N是积极的惯性),可以比较容易地以下述回馈方式界定。

public int factorial(int n)
{
    if (n < 0)
    {
        throw new Exception("A factorial cannot be calculated for negative integers.");
    }

    if (n == 0 || n == 1)
    {
        // end condition, where we do not need to make a recursive call anymore
        return 1;
    }
    else
    {
        // recursive call
        return n * factorial(n - 1);
    }
}

NB:0!

同样,也可以重新界定在特定道路下列举所有档案和文件的方法。 这是因为档案和文件夹有复读结构。

因此,如下方法行之有效:

public static List<FileSystemInfo> GetAllFilesAndFolders(string folder)
{
    // NOTE : We are performing some basic sanity checking
    // on the method s formal parameters here
    if (string.IsNullOrEmpty(folder))
    {
        throw new ArgumentException("An empty string is not a valid path.", "folder");
    }
    if (!Directory.Exists(folder))
    {
        throw new ArgumentException("The string must be an existing path.", "folder");
    }

    List<FileSystemInfo> fileSystemInfos = new List<FileSystemInfo>();

    try
    {
        foreach (string filePath in Directory.GetFiles(folder, "*.*"))
        {
            // NOTE : We will add a FileSystemInfo object for each file found
            fileSystemInfos.Add(new FileInfo(filePath));
        }
    }
    catch
    {
        // NOTE : We are swallowing all exceptions here
        // Ideally they should be surfaced, and at least logged somewhere
        // Most of these will be security/permissions related, i.e.,
        // the Directory.GetFiles method will throw an exception if it
        // does not have security privileges to enumerate files in a folder.
    }
    try
    {
        foreach (string folderPath in Directory.GetDirectories(folder, "*"))
        {
            // NOTE : We will add a FileSystemInfo object for each directory found
            fileSystemInfos.Add(new DirectoryInfo(folderPath));

            // NOTE : We will also add all FileSystemInfo objects found under
            // each directory we find
            fileSystemInfos.AddRange(GetAllFilesAndFolders(folderPath));
        }
    }
    catch
    {
        // NOTE : We are swallowing all exceptions here
        // Ideally they should be surfaced, and at least logged somewhere
        // Most of these will be security/permissions related, i.e.,
        // the Directory.GetDirectories method will throw an exception if it
        // does not have security privileges to enumerate files in a folder.
    }

    return fileSystemInfos;
}

一份行政指示是,这一办法将“按照......全程结构”进行,直至“......”全程“......”。 因此,如果发现许多物体,则可能需要很长时间才能返回。

值得注意的是,使用拉布达语和广度方法可以进一步改善这种方法的可读性。

NB:使用名录的困难。 GetFiles and Directory. 要求再造分数位数的人是,如果存在任何例外(例如,与安全许可有关),这种方法将不退回,而人工休养则允许人们处理这些例外情况,并仍然获得一套档案。





相关问题
热门标签