您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

如何在C#中获取目录中的所有文件、子文件及其大小?

2024/6/8 0:49:44发布37次查看
为了获取文件,c#提供了一个方法directory.getfiles
directory.getfiles返回所有文件的名称(包括它们的路径) ) 匹配指定的搜索模式,并可选择搜索子目录。
在下面的示例中,* 匹配该位置中的零个或多个字符。
searchoption topdirectoryonly。仅搜索顶级目录
searchoption alldirectories .搜索所有顶级目录和子目录
fileinfo 获取文件长度、名称等信息
示例1 h2>static void main (string[] args) { string rootpath = @"c:\users\koushik\desktop\testfolder"; var files = directory.getfiles(rootpath, "*.*", searchoption.alldirectories); foreach (string file in files) { console.writeline(file); } console.readline ();}
输出c:\users\koushik\desktop\testfolder\testfoldermain\testfoldermain.txtc:\users\koushik\desktop\testfolder\testfoldermain 1\testfoldermain1.txtc:\users\koushik\desktop\testfolder\testfoldermain 2\testfoldermain2.txtc:\users\koushik\desktop\testfolder\testfoldermain 2\testfoldermainsubdirectory\testfoldersubdirectory.txt
示例 2static void main (string[] args) { string rootpath = @"c:\users\koushik\desktop\testfolder"; var files = directory.getfiles(rootpath, "*.*", searchoption.topdirectoryonly); foreach (string file in files) { console.writeline(file); } console.readline ();}
输出c:\users\koushik\desktop\testfolder\topdirectory.txt
示例 3static void main (string[] args) { string rootpath = @"c:\users\koushik\desktop\testfolder"; var files = directory.getfiles(rootpath, "*.*", searchoption.alldirectories); foreach (string file in files) { var info = new fileinfo(file); console.writeline($"{ path.getfilename(file) }: { info.length } bytes"); } console.readline ();}
输出topdirectory.txt: 0 bytestestfoldermain.txt: 0 bytestestfoldermain1.txt: 10 bytestestfoldermain2.txt: 20 bytes
以上就是如何在c#中获取目录中的所有文件、子文件及其大小?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product