Home Programs List the Files in a Directory in C#

List the Files in a Directory in C#

In this example we list all of the files in a directory in C#

Example

You need to change the directory – J:\csharp\basics

The files in the directory are printed using directory.getfiles().

Usinga  foreach loop we print the list of files in a directory.

using System;
using System.IO;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] myDirFiles = Directory.GetFiles(@"J:\csharp\basics");
            Console.WriteLine("Files in the Directory");
            foreach (string myFiles in myDirFiles)
            {
                Console.WriteLine(myFiles);
            }
            Console.Read();
        }
    }
}

 

When you run this you will see something like this- this is based on the directory I selected

Files in the Directory
J:\csharp\basics\assigment.cs
J:\csharp\basics\logicaloperators.cs
J:\csharp\basics\relational.cs

You may also like