Newbie: Array of files not shown in textbox

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
 
T

Tim Van Wassenhove

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();
 
C

Cor Ligthert [MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top