简体   繁体   English

我如何将目录内的文件名读取到数组

[英]how do i read the filenames inside the directory to an array

(i am using C# windows application) i want to read all the FileNames of a directory to an array.. how do i read that.. (我正在使用C#Windows应用程序)我想将目录的所有FileName读取到数组中。

( suppose consider a directory with a names ROOT,ROOT2 (假设考虑一个名称为ROOT,ROOT2的目录

Let ROOT1 has a.txt,b.txt,c.txt 假设ROOT1具有a.txt,b.txt,c.txt

let ROOT2 has x.txt,y.txt,z.txt 让ROOT2具有x.txt,y.txt,z.txt

i just want to read those things to my array ... 我只想读那些东西到我的数组...

what is the way to read that...? 用什么方式读那个……? ( or ) can you send me the code for that...? (或)您能给我发送代码吗...?

If there are sub folders you want 如果有您想要的子文件夹

string[] oFiles = Directory.GetFiles(sPath, "*", SearchOption.AllDirectories);

otherwise you want 否则你想要

string[] oFiles = Directory.GetFiles(sPath);

or if you want to filter you want 或者如果您想过滤

string[] oFiles = Directory.GetFiles(sPath, "*");

To filter by .txt extension replace * with *.txt as the second argument. 要按.txt扩展名过滤,请用* .txt作为第二个参数替换*。

This is some code to read the files in a directory: 这是一些读取目录中文件的代码:

DirectoryInfo di = new DirectoryInfo("c:/root1");
FileInfo[] rgFiles = di.GetFiles("*.*");
foreach(FileInfo fi in rgFiles)
{
   Response.Write("<br><a href=" + fi.Name + ">" + fi.Name + "</a>");       
}

FileInfo is a string array containing all files. FileInfo是包含所有文件的字符串数组。

string[] fileNames = Directory.GetFiles(directoryPath, "*", SearchOptions.AllDirectories)

A call to Directory.GetFiles(directoryPath) is what you want. 您需要对Directory.GetFiles(directoryPath)的调用。 If you want to go deeper into the structure of the path (get files in subfolders, etc) then qualify the call with SearchOptions.AllDirectories, or try looking here 如果您想更深入地了解路径结构(在子文件夹中获取文件等),请使用SearchOptions.AllDirectories限定调用,或尝试在此处查找

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

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