Reputation: 23
I can reach with foreach to directory but, because of working like stack, I only reach last picture in the directory. I have lot of image that starting 1.jpg until 100.
namespace deneme_readimg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo("C:\\DENEME");
foreach (FileInfo file in dir.GetFiles())
textBox1.Text = file.Name;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Upvotes: 0
Views: 114
Reputation: 12248
As suggested by @LarsKristensen I'm posting my comment as an answer.
I would use the AppendText method, unless your requirement is to add to the text box on every click I would first make a call to Clear.
namespace deneme_readimg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo("C:\\DENEME");
// Clear the contents first
textBox1.Clear();
foreach (FileInfo file in dir.GetFiles())
{
// Append each item
textBox1.AppendText(file.Name);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Upvotes: 0
Reputation: 32651
for displaying just File name. Use a multiline textbox
StringBuilder sb = new StringBuilder();
foreach (FileInfo file in dir.GetFiles())
sb.Append(file.Name + Environment.NewLine);
textBox1.Text =sb.ToString().Trim();
if you want to show images then you need to use some datacontainer like ListBox
or DataGridView
and add row for each image.
Upvotes: 0
Reputation: 186668
Just collect all the data you need to output in StringBuilder; when ready publish it:
DirectoryInfo dir = new DirectoryInfo("C:\\DENEME");
// Let's collect all the file names in a StringBuilder
// and only then assign them to the textBox.
StringBuilder Sb = new StringBuilder();
foreach (FileInfo file in dir.GetFiles()) {
if (Sb.Length > 0)
Sb.Append(" "); // <- or Sb.AppendLine(); if you want each file printed on a separate line
Sb.Append(file.Name);
}
// One assignment only; it prevents you from flood of "textBox1_TextChanged" calls
textBox1.Text = Sb.ToString();
Upvotes: 0
Reputation: 6132
I am unsure what you are asking or what you are trying to achieve, but if you want to see all of the names, you could change the foreach loop into:
foreach (FileInfo file in dir.GetFiles())
textBox1.Text = textBox1.Text + " " + file.Name;
Upvotes: 1