2013-08-14 3 views
0

foreach를 디렉토리에 연결할 수는 있지만 스택처럼 작동하기 때문에 디렉토리의 마지막 사진에만 도달합니다. 나는 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) 
     { 

     } 
    } 
} 
+1

당신이 할 수있는'textBox1.Text + = file.Name + "";'더 구체적으로 – Satpal

+2

http://msdn.microsoft.com/en-us/library/system.windows.forms합니다. textboxbase.appendtext.aspx –

+0

@DavidMartin +1, 답변으로 게시해야합니다 (약간의 설명이있을 수 있습니다). –

답변

0

합니다.

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

     } 
    } 
} 
1

난 당신이 무엇을 요구하거나 당신이 달성하려고하는 것을 확실하지 오전까지 1.JPG을 시작하는 그 이미지의 여지가 있지만, 모든 이름을보고 싶다면, 당신은을 변경할 수 있습니다 foreach 루프 입력 :

foreach (FileInfo file in dir.GetFiles()) 
    textBox1.Text = textBox1.Text + " " + file.Name; 
0

그냥 파일 이름을 표시합니다. 당신이 이미지를 표시하려면 다음 ListBox 또는 DataGridView 같은 일부 datacontainer를 사용하고 각 이미지에 대한 행을 추가해야 여러 줄 텍스트 상자를

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

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

를 사용합니다.

0

StringBuilder에서 출력해야하는 모든 데이터를 수집하십시오. 준비가 게시 할 때 : @LarsKristensen에 의해 내가 답변으로 내 댓글을 게시하도록하겠습니다 제안으로

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(); 
관련 문제