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
How to Get a JSON String from Url
Incorrect List Model Binding Indices When Using HTML Helpers
Xamarin iOS Memory Leaks Everywhere
How Would You Code an Efficient Circular Buffer in Java or C#
Add "Everyone" Privilege to Folder Using C#.Net
How to Use the Iequalitycomparer
Why Is the Iteration Variable in a C# Foreach Statement Read-Only
Reflection - Getting the Generic Arguments from a System.Type Instance
What Is Linq and What Does It Do
Visual Studio: Contextswitchdeadlock
Copy Constructor Versus Clone()
Getting HTML Body Content in Winforms Webbrowser After Body Onload Event Executes
How to Write an Apple Push Notification Provider in C#
C# Version of Java's Synchronized Keyword
How to Set Up a Web API Controller for Multipart/Form-Data
Using C# Reflection to Call a Constructor
Main Method Code Entirely Inside Try/Catch: Is It Bad Practice