Newbie: Array of files not shown in textbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi,

i am using vs 2005 and i have created a form, which uses DirectoryInfo /
FileInfo in order to find all files in one folder.

now i have created a textbox in the form and want it to be filled with the
output of my files in the relevant folder.

however the following code only shows the last of my files:

{
DirectoryInfo sourceDir = new
DirectoryInfo("C:\\users\\fritz\\desktop");
FileInfo[] files = sourceDir.GetFiles();

int i = 0;

foreach (FileInfo file in files)
{
txtOutput.Text = file;
}
}

instead of " txtOutput.Text = file;" i also used " txtOutput.Text =
files" --> same result.

any idea what is wrong in my code?

thanks in advance, fritz
 
Fritz schreef:
foreach (FileInfo file in files)
{
txtOutput.Text = file;
}
}

For each element, you reassign the txtOutput.Text to file.
Since you're constructing a large string i would recommend to use a
StringBuilder as following:

StringBuilder stringBuilder = new StringBuilder(100);
foreach(FileInfo fileInfo in files) {
stringBuilder.Append(fileInfo);
stringBuilder.Append(" ");
}
txtOutput.Text = stringBuilder.ToString();
 
Fritz,

To do this nice, you have to concatinate the string as Tim already wrote and
than the Stringbuilder can do a greath job. Especially if you put a
/c(arriage) and a /l(inefeed) in it.

However much nicer and easier is it to use for this a listbox,

Cor
 
Back
Top