0

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)
        {

        }
    }
}
4

4 回答 4

1

我不确定您要问什么或您要达到什么目标,但如果您想查看所有名称,可以将 foreach 循环更改为:

foreach (FileInfo file in dir.GetFiles())
    textBox1.Text = textBox1.Text + " " + file.Name; 
于 2013-08-14T07:19:35.347 回答
0

只需在 StringBuilder 中收集您需要输出的所有数据;准备好后发布:

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(); 
于 2013-08-14T07:27:04.377 回答
0

仅显示文件名。使用多行文本框

StringBuilder sb = new StringBuilder();
foreach (FileInfo file in dir.GetFiles())       
   sb.Append(file.Name + Environment.NewLine); 

textBox1.Text =sb.ToString().Trim();

如果要显示图像,则需要使用一些数据容器,例如ListBoxorDataGridView并为每个图像添加行。

于 2013-08-14T07:22:03.150 回答
0

正如@LarsKristensen 所建议的,我将我的评论作为答案发布。

我会使用AppendText方法,除非您的要求是在每次点击时添加到文本框中,我首先会调用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)
        {

        }
    }
}
于 2013-08-14T08:54:35.363 回答