Search This Blog

  • ()
  • ()
Show more
HomeTutorials

Directory.GetFiles returns the file names in a folder

Directory.GetFiles returns the file names in a folder. It is found in the System.IO namespace. It returns a string array—this contains the ...

Directory.GetFiles returns the file names in a folder. It is found in the System.IO namespace. It returns a string array—this contains the full paths of all the files contained inside the specified directory.

String Array

Example. You must include the System.IO namespace with a using directive at the top of your file, or use the fully qualified name System.IO.Directory.GetFiles type. The example program here uses the C: directory.

Also:It filters the files in that directory and only displays the ones with the "BIN" extension.



C# program that gets files in directories

using System;
using System.IO;

class Program
{
static void Main()
{
// Put all file names in root directory into array.
string[] array1 = Directory.GetFiles(@"C:");

// Put all txt files in root directory into array.
string[] array2 = Directory.GetFiles(@"C:", "*.BIN"); // <-- Case-insensitive

// Display all files.

Console.WriteLine("--- Files: ---");
foreach (string name in array1)
{
Console.WriteLine(name);
}

// Display all BIN files.
Console.WriteLine("--- BIN Files: ---");
foreach (string name in array2)
{
Console.WriteLine(name);
}
}
}

Output

--- Files: ---
(All files in your C: folder)
--- BIN Files: ---
(All .bin, .BIN files in your C: folder)

 

This method is static. You do not need to create a Directory instance to use it. The first parameter to the method is the directory path. In a Windows Forms program, you can use Environment.SpecialFolder and FolderBrowerDialog.

EnvironmentFolderBrowserDialog

Note:In ASP.NET, you can use Server.MapPath or the Request.PhysicalApplicationPath property.


MapPath

Next, the second parameter uses the pattern "*.BIN". You have seen these patterns in the "Open File" dialogs in Windows before. The pattern string is case-insensitive, meaning it will match files ending in "BIN", "Bin" and "bin".

 

Discussion. You can get a List collection of the file paths in a directory. To get the List, you will use the same method calls as in the code example. However, you can convert the arrays to Lists with the ToList extension method.

Tip:Include the System.Linq namespace at the top first—it is included by default in new files.


ToListConvert List to ArrayList

Recursively scan folders. Often you will need to get the list of files in a certain directory, and then scan all subdirectories in the folder. There are several ways to accomplish this task, including using true recursion.

Recursive File List

Performance. Directory.GetFiles is fast when used on small directories with few files. But for large directories, with thousands of files, Directory.EnumerateFiles is faster. A benchmark of these methods is available.

EnumerateFiles, GetFiles Benchmark


Summary. We saw the Directory.GetFiles method, which will return the list of files in a specified directory on your hard drive, using the C# language. We tested the method on the C: root directory, and discussed issues related to its usage.

 

 

http://www.dotnetperls.com/directory-getfiles
Name

apple,2,Article,14,At home,13,Author,14,Beauty,1,Biography,2,blackberry,1,Business,6,Cars,5,Celebrity,13,conspiraci,15,Fashion,5,galaxy,1,Gallery,1,Games,11,google,1,Hair,1,Health amp; Fitness,2,Histori,11,Home,169,HOSTING,27,HTML,7,imac,1,Image,1,iphone,1,Itcyber,7,Kuran,31,Lajme,34,Life amp; Love,1,Makeup amp; Skincare,1,mobile,5,monitor,1,Movies,2,News,5,No category,5,Photography,6,PHP,30,Poezi,24,POEZI amp; TEKSTE,2,Post,14,PROGRAMMIM,46,Relationships,1,review,6,samsung,1,Seo,7,Softvare,4,Sport,5,Sports,7,Stars,5,Tag,7,Tags,7,Tech,30,Teknologji,37,THEMES,3,Tutorials,191,Video,47,Videos,4,Vip News,2,Webhosting,67,WordPress,7,World,13,
ltr
item
Cr337: Directory.GetFiles returns the file names in a folder
Directory.GetFiles returns the file names in a folder
Cr337
https://cr337.blogspot.com/2015/05/directorygetfiles-returns-file-names-in.html
https://cr337.blogspot.com/
http://cr337.blogspot.com/
http://cr337.blogspot.com/2015/05/directorygetfiles-returns-file-names-in.html
true
5516280490839897951
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy