2016-06-30 10 views
1

からフォルダ(名前を)読み:C#は、私はこれらのコードを持っているディレクトリ

 string directory; 
     FolderBrowserDialog fbd = new FolderBrowserDialog(); 
     if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
      directory = fbd.SelectedPath; 

      txtSource.Text = directory; 

      DirectoryInfo d = new DirectoryInfo(directory); 
      FileInfo[] Files = d.GetFiles(); 

      List<String> str = new List<string>(); 
      foreach (FileInfo file in Files) 
      { 
       str.Add(file.Name); 
      } 

     } 

を私は、フォルダのパスを選択しFolderBrowseDialogを持っています。 この選択されたフォルダには、3つの他のフォルダがあります。私はこのフォルダの名前を読みたいと思う。私は知りたい、またはいくつかのファイルを読んでいません。

ありがとうございます。

答えて

2

をあなたはDirectory.GetDirectories()を使用することができます。

string[] subdirs = Directory.GetDirectories(fbd.SelectedPath); 

これはあなたのサブディレクトリへの完全なパスを提供します。あなただけのサブフォルダの名前ではなく、完全なパスが必要な場合は、`Path.GetFileName()を使用することができます。

string[] subdirs = Directory.GetDirectories(fbd.SelectedPath) 
          .Select(Path.GetFileName) 
          .ToArray(); 

それとも両方が必要な場合:

var subdirs = Directory.GetDirectories(fbd.SelectedPath) 
          .Select(p => new { 
           Path = p, 
           Name = Path.GetFileName(p)}) 
          .ToArray(); 
+0

[OK]を、私はどのようにすることができ、パスを持っていますパスと名前を取得しますか? – MMbach

+0

@MMbachは答えを –

+0

更新しました – MMbach

関連する問題