As you can see by the commented code below, I’m trying to display the name and checksum for each file in a folder inside a message box. The problem is that this pops up a separate message box for each file, rather than displaying them all in one window. I realize that I need to move the MessageBox.Show() line outside of the foreach loop, but then it only displays the last file and not all of them.
What would be the code to display all the files in one message box?
// for each file in selected folder, print out its name and MD5 checksum value
foreach (string file in Directory.GetFiles(folderBrowserDialog1.SelectedPath))
{
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(file))
{
checksum = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
MessageBox.Show(file + ": " + checksum);
}
}
}
Use a StringBuilder and then append the file/checksum to the stringbuilder object during each iteration, then call
MessageBox.Show()on the stringbuilder object, for example:Or use:
Inside the loop to avoid the overhead of creating a new string for each iteration of the loop *credit goes to Destrictor*