Home  >  Article  >  Backend Development  >  How to list all available files in a directory using C#?

How to list all available files in a directory using C#?

PHPz
PHPzforward
2023-09-12 18:41:08658browse

如何使用 C# 列出目录中的所有可用文件?

First, use the DirectoryInfo object-

//creating a DirectoryInfo object
DirectoryInfo mydir = new DirectoryInfo(@"d:\amit");

Now, use the GetFiles() method to get all the files-

FileInfo [] f = mydir.GetFiles();

To get the list of files in the directory , please try running the following code -

Example

using System;
using System.IO;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         //creating a DirectoryInfo object
         DirectoryInfo mydir = new DirectoryInfo(@"d:\amit");

         // getting the files in the directory, their names and size
         FileInfo [] f = mydir.GetFiles();

         foreach (FileInfo file in f) {
            Console.WriteLine("File Name: {0} Size: {1}", file.Name, file.Length);
         }
         Console.ReadKey();
      }
   }
}

The above is the detailed content of How to list all available files in a directory using C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete