C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Here: The program uses the C:\ directory. It filters the files in that directory and only displays the ones with the "BIN" extension.
Literals: More information about the string literal format used is available—the verbatim literals are useful for paths.
String LiteralArgument 1: The first argument to GetFiles is the directory path. In a Windows Forms program, you can use Environment.SpecialFolder.
EnvironmentArgument 2: The second argument uses the pattern "*.BIN". You have seen these patterns in the "Open File" dialogs in Windows before.
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 bin files in root directory into array.
// ... This is case-insensitive.
string[] array2 = Directory.GetFiles(@"C:\", "*.BIN");
// 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)
Tip: Include the System.Linq namespace at the top first—it is included by default in new files.
ToListConvert List, ArrayListC# program that gets list of files
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Program
{
static void Main()
{
string[] array1 = Directory.GetFiles(@"C:\");
// Get list of files.
List<string> filesList = array1.ToList();
Console.WriteLine(filesList.Count);
}
}
Output
14
Tip: EnumerateFiles can be faster for large result lists. But for small directories, GetFiles may be faster.
C# program that uses Directory.EnumerateFiles
using System;
using System.IO;
class Program
{
static void Main()
{
// Loop over all files in C directory.
foreach (string path in Directory.EnumerateFiles("C:\\"))
{
Console.WriteLine("IN C DIRECTORY: " + path);
}
}
}
Output
IN C DIRECTORY: C:\pagefile.sys
IN C DIRECTORY: ...
IN C DIRECTORY: C:\swapfile.sys
IN C DIRECTORY: ...
Note: EnumerateFiles is helpful on a recursive directory scan, as the result count might be large.
Here: We call EnumerateFiles to get all the deeply-nested files in directories. Notice the result files have different directories.
C# program that uses SearchOption.AllDirectories
using System;
using System.IO;
class Program
{
static void Main()
{
// Recursively get file names for all files in a directory.
// ... Use EnumerateFiles to accommodate large result count.
foreach (string file in Directory.EnumerateFiles(@"c:\go\test\bench\",
"*.*",
SearchOption.AllDirectories))
{
Console.WriteLine("IN DIRECTORY: " + file);
}
}
}
Output
IN DIRECTORY: c:\go\test\bench\garbage\Makefile
IN DIRECTORY: ...
IN DIRECTORY: c:\go\test\bench\go1\binarytree_test.go
IN DIRECTORY: ...