从递归文件夹中获取文件信息

时间:2015-02-21 14:47:37

标签: c# winforms

我想要做的是从文件夹中的每个文件及其下方的所有文件夹中获取6个文件信息。这些作品是:

  • 类型(您可以在Access中执行此操作,它将返回" Microsoft Excel工作表"或者" Winzip文件"或者某些内容,就像您在资源管理器窗口中看到的那样,但我没有& #39;在C#中看到这个属性
  • 全名
  • 长度
  • CREATIONTIME
  • 的LastAccessTime
  • LastWriteTime

我有以下代码,它会吐出一个带路径的文件列表,但是我试图找出如何在输出中获取其他信息。如果它可以用分号或其他东西分隔,那将是好的,因为最终的愿望是将此信息写入表格。任何帮助表示赞赏。

    private void GetMyFiles () {
        string rootDirectory = @"" + txtPathToScan.Text + "";
        string extension = "*";

        List<string> files = GetFilesRecursively (rootDirectory, extension);

        Console.WriteLine ("Got {0} files of extension {1} in directory {2}", files.Count, extension, rootDirectory);
        foreach (string file in files) {
            Console.WriteLine (file);
        }
    }

    /// <summary>
    /// This function calls itself recursively for each of the subdirectories that it finds
    /// in the root directory passed to it. It returns files of the extension as specified
    /// by the caller.
    /// </summary>
    /// <param name="rootDirectory">The directory from which the file list is sought.</param>
    /// <param name="extension">The particular extension for which the file list is sought.</param>
    /// <returns>A list of all the files with extension as specified by the caller. This list
    /// includes the files in the current directory as well its sub-directories.</returns>
    static List<string> GetFilesRecursively (string rootDirectory, string extension) {
        // Uncomment this line only if you want to trace the control as it goes from
        // sub-directory to sub-directory. Be ready for long scrolls of text output:
        //Console.WriteLine ("Currently in directory {0}", rootDirectory);

        // Create an output list:
        List<string> opList = new List<string> ();

        // Get all files in the current directory:
        string[] allFiles = Directory.GetFiles (rootDirectory, "*." + extension);

        // Add these files to the output list:
        opList.AddRange (allFiles);

        // Get all sub-directories in current directory:
        string[] subDirectories = Directory.GetDirectories (rootDirectory);

        // And iterate through them:
        foreach (string subDir in subDirectories) {
            // Get all the files from the sub-directory:
            List<string> subDirFileList = GetFilesRecursively (subDir, extension);
            // And add it to this list:
            opList.AddRange (subDirFileList);
        }

        // Finally return the output list:
        return opList;
    }

2 个答案:

答案 0 :(得分:1)

你可以使用FileInfo类......

获得大部分内容
var info = new System.IO.FileInfo(@"c:\folder\file.ext");

它具有LengthCreationTimeLastAccessTimeLastWriteTime的属性,可以将它们(例如)输出到控制台,您可以...

Console.WriteLine("For file {0}", info.Name);
Console.WriteLine("Length is {0}", info.Length);
Console.WriteLine("Creation time is {0}", info.CreationTime);
Console.WriteLine("Last access time is {0}", info.LastAccessTime);
Console.WriteLine("Last write time is {0}", info.LastWriteTime);

要获取完整文件名,您可以使用GetFileNameWithoutExtension ...

var fileName = System.IO.Path.GetFileNameWithoutExtension(@"c:\folder\file.ext")
在上面的示例中,

filename将等于"file"

获取文件扩展名的说明要多一些,但可以使用SHGetFIleInfo完成,有关详细信息,请参阅this answer

要将其与您当前的代码集成(如果您不那么担心文件说明),可以更改GetFilesRecursively方法以返回List<FileInfo> ...

static List<FileInfo> GetFilesRecursively (string rootDirectory, string extension)

相应更新opList变量...

List<FileInfo> opList = new List<FileInfo> ();

然后调整AddRange来电添加FileInfo个对象......

opList.AddRange (allFiles.Select(f => new FileInfo(f)));

最后,subDirFileList的类型也需要更改,因此List<FileInfo>

(我认为这就是一切!)

答案 1 :(得分:0)

对于后人,完成的答案如下。如果您要投票,请投票给理查德的答案。谢谢!

    private void GetMyFiles () {
        string rootDirectory = @"" + txtPathToScan.Text + "";
        string extension = "*";

        List<FileInfo> files = GetFilesRecursively (rootDirectory, extension);

        Console.WriteLine ("Got {0} files of extension {1} in directory {2}", files.Count, extension, rootDirectory);
        foreach (FileInfo file in files)
        {
            Console.WriteLine(file);
            var info = new System.IO.FileInfo(@"" + file + "");

            Console.WriteLine("For file {0}", info.Name);
            Console.WriteLine("Length is {0} KB", info.Length/1024);
            Console.WriteLine("Creation time is {0}", info.CreationTime);
            Console.WriteLine("Last access time is {0}", info.LastAccessTime);
            Console.WriteLine("Last write time is {0}", info.LastWriteTime);

        }
    }

    /// <summary>
    /// This function calls itself recursively for each of the subdirectories that it finds
    /// in the root directory passed to it. It returns files of the extension as specified
    /// by the caller.
    /// </summary>
    /// <param name="rootDirectory">The directory from which the file list is sought.</param>
    /// <param name="extension">The particular extension for which the file list is sought.</param>
    /// <returns>A list of all the files with extension as specified by the caller. This list
    /// includes the files in the current directory as well its sub-directories.</returns>
    static List<FileInfo> GetFilesRecursively(string rootDirectory, string extension)
    {
        // Uncomment this line only if you want to trace the control as it goes from
        // sub-directory to sub-directory. Be ready for long scrolls of text output:
        //Console.WriteLine ("Currently in directory {0}", rootDirectory);

        // Create an output list:
        List<FileInfo> opList = new List<FileInfo>();

        // Get all files in the current directory:
        string[] allFiles = Directory.GetFiles (rootDirectory, "*." + extension);

        // Add these files to the output list:
        opList.AddRange(allFiles.Select(f => new FileInfo(f)));

        // Get all sub-directories in current directory:
        string[] subDirectories = Directory.GetDirectories (rootDirectory);

        // And iterate through them:
        foreach (string subDir in subDirectories) {
            // Get all the files from the sub-directory:
            List<FileInfo> subDirFileList = GetFilesRecursively (subDir, extension);
            // And add it to this list:
            opList.AddRange (subDirFileList);
        }

        // Finally return the output list:
        return opList;
    }