1

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);
           }
       }
   }
svick
  • 236,525
  • 50
  • 385
  • 514
user1985189
  • 669
  • 5
  • 16
  • 25
  • -1 because you should know that if you are in a for loop or a while loop or a foreach loop, that the MessageBox will display everytime.. I would suggest you looking up how to use the string.Join() method and throwing the MessageBox.Show() Method outside of the foreach loop – MethodMan Feb 05 '13 at 15:19
  • I do know that. I said so explicitly... – user1985189 Feb 05 '13 at 15:20

3 Answers3

3

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:

StringBuilder s = new StringBuilder();
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();
            s.AppendLine(file + ": " + checksum);
        }
    }
}
MessageBox.Show(s.ToString());

Or use:

s.AppendFormat("{0}: {1}", file, checksum).AppendLine();

Inside the loop to avoid the overhead of creating a new string for each iteration of the loop *credit goes to Destrictor*

DGibbs
  • 14,316
  • 7
  • 44
  • 83
  • I don't like you using stringbuilder and then using `string + string` : – Destrictor Feb 05 '13 at 15:23
  • 2
    +1Destrictor if you don't like it then provide a better solution DGibbs has provided a nice solution – MethodMan Feb 05 '13 at 15:25
  • @DJKRAZE I honestly don't see the point of using stringbuilder if you're going to use plain string concatenation in a tight loop anyways >.> – Destrictor Feb 05 '13 at 15:33
  • @Destrictor what would you recommend as an alternative in this scenario? The compiler will use a StringBuilder internally anyway... – DGibbs Feb 05 '13 at 15:58
  • @DGibbs I'm not sure it does, see [here](http://stackoverflow.com/a/2876972/1730951), but if it does, it's creating an additional stringbuilder object for every loop (generally you'll need >= 10 strings concatenated to achieve better performance with stringbuilders). I'd suggest using `s.AppendFormat("{0}: {1}", file, checksum).AppendLine();` It's a bit longer, but more in the spirit with stringbuilder. – Destrictor Feb 06 '13 at 09:01
  • 1
    @Destrictor, no it definitely does if it's inside a loop, there are some answers in your link that confirm that too. Though i do like your suggestion i don't think the OP is all that concerned (though he should be). I've edited in your amendments – DGibbs Feb 06 '13 at 09:22
1

The easiest way to do this is to create a List variable and add each string you want to display. After the foreach loop you add a line: MessageBox.Show(string.Join(Environment.NewLine,MyList.ToArray())); That should do the trick.

NDraskovic
  • 706
  • 2
  • 22
  • 50
0

How about declaring a string variable string foo = ""; before the loop, and adding this line of code to the inside of the loop:

foo += file + ": " + checksum + System.Environment.NewLine;

And, after the loop:

MessageBox.Show(foo);
Conduit
  • 2,675
  • 1
  • 26
  • 39
  • 1
    in many cases "string + string" is slower then StringBuilder.Append or String.Format(). – Offler Feb 05 '13 at 15:25
  • @Offler Thanks for the tip! I've only recently started using C#; I'll definitely look into the StringBuilder class. – Conduit Feb 05 '13 at 15:29