Getting All File Names from a Folder Using C#

How do I retrieve all filenames in a directory?

foreach (string s in Directory.GetFiles(path, "*.txt").Select(Path.GetFileName))
Console.WriteLine(s);

How can i get all files from a given file name string path?

To get a parent folder of a specifique file just use this line Path.GetDirectoryName(fileName);

You can get all files that match bmp, jpeg or tiff extensions just use the following code Directory.EnumerateFiles. Finally you must have something like this code ;

var directory = System.IO.Path.GetDirectoryName(fileName);
var files = System.IO.Directory.EnumerateFiles(directory, "*.*")
.Where(s => s.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase)
|| s.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase)
|| s.EndsWith(".tiff ", StringComparison.OrdinalIgnoreCase));

Directory.GetFiles: how to get only filename, not full path?

You can use System.IO.Path.GetFileName to do this.

E.g.,

string[] files = Directory.GetFiles(dir);
foreach(string file in files)
Console.WriteLine(Path.GetFileName(file));

While you could use FileInfo, it is much more heavyweight than the approach you are already using (just retrieving file paths). So I would suggest you stick with GetFiles unless you need the additional functionality of the FileInfo class.

How to get only filenames within a directory using c#?

You can use Path.GetFileName to get the filename from the full path

private string[] pdfFiles = Directory.GetFiles("C:\\Documents", "*.pdf")
.Select(Path.GetFileName)
.ToArray();

EDIT: the solution above uses LINQ, so it requires .NET 3.5 at least. Here's a solution that works on earlier versions:

private string[] pdfFiles = GetFileNames("C:\\Documents", "*.pdf");

private static string[] GetFileNames(string path, string filter)
{
string[] files = Directory.GetFiles(path, filter);
for(int i = 0; i < files.Length; i++)
files[i] = Path.GetFileName(files[i]);
return files;
}

How to get files names in multiple folders under some condition

Use DirectoryInfo:

using System.Collections.Generic;
using System.IO;

List<FileInfo> files = new List<FileInfo>();
DirectoryInfo rootDir = new DirectoryInfo(@"C:\Program Files (x86)\basic\data\");
var directories = rootDir.GetDirectories("20160314*");
foreach (var directory in directories)
{
files.AddRange(directory.GetFiles());
}

IEnumerable<string> fileNames = files.Select(f => f.Name);

use IEnumerable<string> fileNames = files.Select(f => f.FullName); to get the file name with path.

Method to get all files within folder and subfolders that will return a list

private List<String> DirSearch(string sDir)
{
List<String> files = new List<String>();
try
{
foreach (string f in Directory.GetFiles(sDir))
{
files.Add(f);
}
foreach (string d in Directory.GetDirectories(sDir))
{
files.AddRange(DirSearch(d));
}
}
catch (System.Exception excpt)
{
MessageBox.Show(excpt.Message);
}

return files;
}

and if you don't want to load the entire list in memory and avoid blocking you may take a look at the following answer.

How to extract all file names out of a folder in c# and display them in a text file or csv

Please see below code for your query. You can use this code in console application and use it.

    static void Main(string[] args)
{
GetFileNamesandWriteInTxt();
}

public static void GetFileNamesandWriteInTxt()
{
string path = @'X:\myfolder\photos';
DirectoryInfo dr = new DirectoryInfo(path);
FileInfo[] mFile = dr.GetFiles();
StreamWriter writer = new StreamWriter("D:\\FileNames.txt", true);
foreach (FileInfo fiTemp in mFile)
{
writer.WriteLine(fiTemp.Name);
Console.WriteLine(fiTemp.Name);
}
Console.ReadLine();
}

Hope this will work for you!

Thanks,
Anshul



Related Topics



Leave a reply



Submit