string folderPath = "C:\\Personal\\Files";
if (Directory.Exists(folderPath))
{
string searchFileName = "F3.txt";
string filePath = Path.Combine(folderPath, searchFileName);
if (File.Exists(filePath))
{
Console.Write($"Folder exists {searchFileName} in folder.");
}
else
{
Console.WriteLine("File does not exists.");
}
}
else
{
Console.WriteLine("Folder does not exists.");
}
In above code I have taken the folder path and. After that I have validated whether the folder exists or not by using Directory.Exists(folderPath). If it exists on that case, we have taken the file name in a variable which we want to search here we have taken file name "F3.txt". To search the file from folder File.Exists(filePath) is used. If file exists on that case, we will display the message to user that file exists, on other hand will display message that file does not exists. To prepare the file path i have used Path.Combine.
How to Search a Specific File in a Directory Using C#.Net?
How to search a file available in folder locally using c#.net in .net core. Here system will validate folder exists or not and then check for file.
In today's article I will show you how you can search a specific file from a directory using c#.net in .net core. So here I have used .net core 8 and console application for searching a file. So first we will we create a directory and add some files in it.
From this list we will search the file. Now let's write the code to search the file from the given location.
