计算文件扩展名C# [英] Counting Files Extensions C#

查看:96
本文介绍了计算文件扩展名C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ac#初学者,我想做一个简单的程序,它将通过一个文件夹,并计算.mp3文件和.flac文件的数量。

Hi i'm a c# 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 .

就像我说的那样,该程序非常基础。它将询问音乐文件夹路径,然后通过它。我知道该主音乐文件夹中会有很多子文件夹,因此它一次必须打开一个子文件夹并进行浏览。

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:/音乐/
将成为给定目录。
但是它本身不包含任何音乐。
要访问音乐文件,程序必须打开子文件夹,如

C:/Music/ will be the given directory. 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 /
只有这样他可以计算.mp3文件和.flac文件并将它们存储在两个单独的计数器中。
该程序必须至少对2000个文件夹执行此操作。

C:/Music/Electronic/deadmau5/RandomAlbumTitle/ Only then he can count the .mp3 files and .flac files and store them in two separated counters. The program will have to do that for at least 2000 folders.

您知道浏览文件并返回其名称的好方法或方法吗(扩展名)?

Do you know a good way or method to go through files and return its name (and extension)?

推荐答案

您可以使用 System.IO.DirectoryInfo DirectoryInfo 提供了 GetFiles 方法,该方法也具有递归选项,因此,如果您不担心速度,可以执行以下操作:

You can use 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 di = new DirectoryInfo(@"C:\Music");

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

这篇关于计算文件扩展名C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆