LoginSignup
0
0

More than 5 years have passed since last update.

指定したディレクトリのファイルの名前一覧を表示する

Posted at

ディレクトリの中にあるファイル名を取得する

勉強用のメモとして書きます。

環境

Visual Studio Community 2017

方法

GetFilesメソッドでファイルを取得します。
第一引数は指定したディレクトリのパス、第二引数はファイルの条件を指定することができます。

ソースコード

public class ReadFiles
{
    private string filePath;

    public ReadFiles(string path)
    {
        this.filePath = path;
        var files = Directory.GetFiles(path);
        for (int i = 0; i < files.Length; i++)
        {
            Console.WriteLine(files[i]);
        }
    }
}

これでReadFilesクラスのオブジェクト作る際に引数にディレクトリのパスを指定することで、そのディレクトリ内にある拡張子がtxtのファイル名の一覧を取得することできます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0