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

PHPz
Release: 2023-09-12 18:41:08
forward
669 people have browsed it

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

First, use the DirectoryInfo object-

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

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

FileInfo [] f = mydir.GetFiles();
Copy after login

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();
      }
   }
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template