Click here to Skip to main content
15,908,166 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
md5 of files are:
a.exe :8A1C8273F25E920CA8809107B069AC8D
a1.exe : A8EF444D951FA378479A7C9D9611B6CC
2 files are in desktop

My code here:
private void Button6_Click(object sender, EventArgs e)
        {
            string[] source = new string[] { "8A1C8273F25E920CA8809107B069AC8D", "A8EF444D951FA378479A7C9D9611B6CC"};

            //string vr = 
            var files = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
            try
            {
                foreach(var vr in files)
                {
                    
                    
                    var md5String = CreateMD5StringFromFile(vr); textBox1.Text = md5String.ToString();
                    if (source.Contains(md5String))
                    {
                        listBox1.Items.Add(vr);
                    }
                }

            }
            catch (Exception) { }
        }

when button6 clicked, there only a.exe added to listbox
help

What I have tried:

I think the reason is using if in foreach
is there solution for this?
Posted
Updated 28-Jul-19 13:24pm

At a guess, one of the files it returns does not have the right permissions for you to read it - so your try ... catch code catches the exception and that exits the for loop - there are several files in "normal folders" which are system only, and which will cause this behaviour. As will a file being open for exclusive access by a different app.

And because your code swallows the exception, you can never see that, except with the debugger - and even then you can't see what the file names that cause the problem might be once the exception occurs because it's all gone out of scope by then.

Stop swallowing exceptions: it isn't a "miracle cure" to fix your app crashing, it's a way to tell you "there is a problem, and you haven't dealt with it". Swallowing it exception doesn't fix that, it just hides it for later on when it causes even more problems - as you have just discovered...
 
Share this answer
 
Quote:
when button6 clicked, there only a.exe added to listbox

The behavior is perfectly expected if second md5 is not what you expect.
We can't do anything for you, only the debugger can help you.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
It's definitely NOT because you're using an IF in a FOREACH. There is nothing wrong, nor special about an IF being used in a FOREACH block.

Use the debugger and step through the code line by line and examine the content of the variables. I'm sure you'll find the values in the variables are not exactly as you expect and that's why you're getting only one value added to your listbox.
 
Share this answer
 
Try moving the Try-Catch inside the ;foreach loop:
private void Button6_Click(object sender, EventArgs e)
{
    string[] source = new string[] { "8A1C8273F25E920CA8809107B069AC8D", "A8EF444D951FA378479A7C9D9611B6CC"};

    //string vr = 
    var files = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));

    foreach(var vr in files)
    {
        try
       { 
            var md5String = CreateMD5StringFromFile(vr); textBox1.Text = md5String.ToString();

            if (source.Contains(md5String))
            {
                listBox1.Items.Add(vr);
            }
        }                 
        catch (Exception ex)
        {
            Console.WriteLine($"error in file: {vr.FullName} | {ex.Message}");
        };
    }
}
You can run this as a "diagnostic" to see the errors that may be happening. After that, you should write specific handlers for the Types of errors thrown. I really agree with "never swallow an error you have not written an explicit catch for" ... except in debugging.

This thread on StackOverFlow has some interesting examples of newer ways in C#6 and #7 for handling catching multiple error types: [^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900