简体   繁体   中英

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..

( suppose consider a directory with a names ROOT,ROOT2

Let ROOT1 has a.txt,b.txt,c.txt

let ROOT2 has 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.

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.

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

A call to Directory.GetFiles(directoryPath) is what you want. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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