How to Search For a File in a Directory Using C # (C Sharp)


go to site This article, which is mainly a code snippet, will show you how to search for a file in a given directory. As you can see in the main method the search name is “win” which is the file name we are looking for and the folder is “C: Program Files”. The SearchDirectories method takes these two parameters and does the actual searching.

see url using System;
using System.IO;
public class FileSearch {
public static void Main () {
// The name to search for
String searchName = “win”;
// The directory to search in.
DirectoryInfo myDir = new DirectoryInfo (@ “c: Program Files”);
SearchDirectories (myDir, searchName);

source url public static void SearchDirectories (DirectoryInfo dir, String target) {
FileInfo [] files = dir.GetFiles ();
foreach (FileInfo file in files) {
if (file.Name.IndexOf (target)> -1) {
Console.WriteLine (file.Name);

watch DirectoryInfo [] dirs = dir.GetDirectories ();
// Searching in the subdirectories as well.
foreach (DirectoryInfo subDir in dirs) {
SearchDirectories (subDir, target);

here This method is recursive because it calls itself, making it very efficient for searching through directories. This is necessary since it also searches each sub directory in a directory. So if you have a folder inside of a folder it will search for all the files in that folder as well.

click here The result of running this program will just be an output of the file names because all were doing with the file is: Console.WriteLine (file.Name); This could be changed to something different such as deleting or moving the files, making this program a very compact and powerful program that can make menial tasks take much less time.

enter Immobilienmakler Heidelberg

enter Makler Heidelberg


Immobilienmakler Heidelberg

Makler Heidelberg

Der Immoblienmakler für Heidelberg Mannheim und Karlsruhe
Wir verkaufen für Verkäufer zu 100% kostenfrei
Schnell, zuverlässig und zum Höchstpreis

Source by Curtis M Larson