C# Program to Search Directories and List Files
Posted by Superadmin on August 13 2022 08:08:43

C# Program to Search Directories and List Files

 

This is a C# Program to search directories and list files.

Problem Description

This C# Program Searches Directories and List Files.

Problem Solution

Here the particular given directory is searched and all the files are listed.

Program/Source Code

Here is source code of the C# Program to Search Directories and List Files. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Search Directories and List Files
 */
using System;
using System.IO;
class Program
{
    static void Main()
    {
        string[] Dirfile = Directory.GetFiles("C:\\sri\\","*.*",
                                              SearchOption.AllDirectories);
        foreach (string file in Dirfile)
        {
            Console.WriteLine(file);
        }
        Console.Read();
    }
}
Program Explanation

This C# program is used to search directories and list files. Here the particular given directory is searched and all the files are listed. Using foreach loop print the directories and list files.

 
Runtime Test Cases
 
The List of Files in the Directory are :
C:\sri\message.txt
C:\sri\srip.txt
C:\sri\test.txt