简体   繁体   English

计算文件扩展名C#

[英]Counting Files Extensions C#

Hi i'm ac# begginer and i'd like to do a simple program which is going to go through a folder and count how many files are .mp3 files and how many are .flac . 嗨,我是ac#begginer,我想做一个简单的程序,它将通过一个文件夹计算有多少文件是.mp3文件和多少是.flac。

Like I said the program is very basic. 就像我说的那样,程序非常基础。 It will ask for the music folder path and will then go through it. 它将询问音乐文件夹路径,然后将通过它。 I know there will be a lot of subfolders in that main music folder so it will have to open them one at the time and go through them too. 我知道主音乐文件夹中会有很多子文件夹,所以它必须在当时打开它们并通过它们。

Eg 例如

C:/Music/ will be the given directory. C:/ Music /将是给定目录。 But it doesn't contain any music in itself. 但它本身并不包含任何音乐。 To get to the music files the program would have to open subfolders like 要获取音乐文件,程序必须打开子文件夹

C:/Music/Electronic/deadmau5/RandomAlbumTitle/ Only then he can count the .mp3 files and .flac files and store them in two separated counters. C:/ Music / Electronic / deadmau5 / RandomAlbumTitle /只有这时他才能计算.mp3文件和.flac文件并将它们存储在两个独立的计数器中。 The program will have to do that for at least 2000 folders. 该程序必须为至少2000个文件夹执行此操作。

Do you know a good way or method to go through files and return its name (and extension)? 您是否知道一种通过文件并返回其名称(和扩展名)的好方法或方法?

You can use System.IO.DirectoryInfo . 您可以使用System.IO.DirectoryInfo DirectoryInfo provides a GetFiles method, which also has a recursive option, so if you're not worried about speed, you can do this: DirectoryInfo提供了一个GetFiles方法,它也有一个递归选项,所以如果你不担心速度,你可以这样做:

DirectoryInfo di = new DirectoryInfo(@"C:\Music");

int numMP3 = di.GetFiles("*.mp3", SearchOption.AllDirectories).Length;
int numFLAC = di.GetFiles("*.flac", SearchOption.AllDirectories).Length;

Use DirectoryInfo and a grouping by the file extension: 使用DirectoryInfo和文件扩展名分组:

var di = new DirectoryInfo(@"C:/Music/");
var extensionCounts = di.EnumerateFiles("*.*", SearchOption.AllDirectories)
                        .GroupBy(x => x.Extension)
                        .Select(g => new { Extension = g.Key, Count = g.Count() })
                        .ToList();

foreach (var group in extensionCounts)
{
    Console.WriteLine("There are {0} files with extension {1}", group.Count, 
                                                                group.Extension);
}

C# has a built in method of searching for files in all sub-directories. C#有一个内置的方法来搜索所有子目录中的文件。 Make sure you add a using statement for System.IO 确保为System.IO添加using语句

var path = "C:/Music/"
var files = Directory.GetFiles(path, "*.mp3", SearchOption.AllDirectories);
var count = files.Length;

Since you're a beginner you should hold off on the more flexible LINQ method until later. 既然你是初学者,你应该暂时停止使用更灵活的LINQ方法。

int fileCount = Directory.GetFiles(_Path, "*.*", SearchOption.TopDirectoryOnly).Length

Duplicate question How to read File names recursively from subfolder using LINQ 重复的问题如何使用LINQ从子文件夹递归读取文件名

Jon Skeet answered there with Jon Skeet回答说

You don't need to use LINQ to do this - it's built into the framework : 您不需要使用LINQ来执行此操作 - 它内置于框架中

string[] files = Directory.GetFiles(directory, "*.dll",
                                    SearchOption.AllDirectories);

or if you're using .NET 4 : 或者如果您使用的是.NET 4

IEnumerable<string> files = Directory.EnumerateFiles(directory, "*.dll",
                                                    SearchOption.AllDirectories);

To be honest, LINQ isn't great in terms of recursion. 说实话,LINQ在递归方面并不是很好。 You'd probably want to write your own general-purpose recursive extension method. 您可能想要编写自己的通用递归扩展方法。 Given how often this sort of question is asked, I should really do that myself some time... 考虑到这类问题经常被问到,我自己应该这样做一段时间......

Here is MSDN support page, How to recursively search directories by Visual C# 这是MSDN支持页面, 如何通过Visual C递归搜索目录#

Taken directly from that page: 直接从该页面采取:

void DirSearch(string sDir) 
{
    try 
    {
       foreach (string d in Directory.GetDirectories(sDir)) 
       {
        foreach (string f in Directory.GetFiles(d, txtFile.Text)) 
        {
           lstFilesFound.Items.Add(f);
        }
        DirSearch(d);
       }
    }
    catch (System.Exception excpt) 
    {
        Console.WriteLine(excpt.Message);
    }
}

You can use this code in addition to creating FileInfo objects. 除了创建FileInfo对象之外,您还可以使用此代码。 Once you have the file info objects you can check the Extension property to see if it matches the ones you care about. 获得文件信息对象后,可以检查Extension属性以查看它是否与您关注的属性相匹配。

MSDN有很多信息和示例,例如如何遍历目录: http//msdn.microsoft.com/en-us/library/bb513869.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM