ceranda
ceranda

Reputation: 61

C#, foreach values to TextBox

From below given foreach loop I'm getting all the file names in a folder. I want to know how to put all the file names in text box. According to below code only the last file name appear in textbox.

private void btnGetFileNames_Click(object sender, EventArgs e)
    {
      DirectoryInfo dinf = new DirectoryInfo(tbxFileLocation.Text);

      foreach (FileInfo Fi in dinf.GetFiles())
        {
            tbxFileList.Text=Fi.ToString();  
        }
    }

Upvotes: 1

Views: 764

Answers (2)

Amol Bavannavar
Amol Bavannavar

Reputation: 2062

Try this :

private void btnGetFileNames_Click(object sender, EventArgs e)
{
  DirectoryInfo dinf = new DirectoryInfo(tbxFileLocation.Text);

  foreach (FileInfo Fi in dinf.GetFiles())
    {
        tbxFileList.Text+=Fi.ToString() + Environment.NewLine;  
    }
}

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222522

Use a StringBuilder and append the filenames to it, Display finally

StringBuilder filenames = new StringBuilder();
foreach (FileInfo Fi in dinf.GetFiles())
  {
      filenames.Append(Fi.ToString());
      filenames.Append(",");           
   }
tbxFileList.Text=filenames.ToString();  

Upvotes: 3

Related Questions