Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
How to check in all PC hard drives, if a folder exists ?
I would like to control at one time and display a message that says yes, find in such disc.
In my example, the for condition displays as much message as it finds disk ...
How to search without a for loop ?
Thank you


What I have tried:

C#
<pre>          string[] str1 = Directory.GetLogicalDrives();
                    // for (initialization, condition, iteration)
                    for (int i = 0; i < str1.Length; ++i)
                    //  MessageBox.Show(str1[i]);

                    if (Directory.Exists(str1[i] + "Dossier1"))
                    {
                        MessageBox.Show("exists in : " + str1[i]);
                    }
                    else
                    {

                        MessageBox.Show("does not exist in : " + str1[i]);

                    }
Posted
Updated 27-Apr-19 20:31pm
v2
Comments
BillWoodruff 30-Apr-19 4:46am    
I suggest you read this: https://blogs.msdn.microsoft.com/jaredpar/2009/12/10/the-file-system-is-unpredictable/

And this: https://stackoverflow.com/a/11710169/133321

1) Save the answer until after the loop, then display a single "yes / no" message.
Or
2) Use Linq methods:
C#
string[] hasFolder = Directory.GetLogicalDrives().Where(d => Directory.Exists(Path.Combine(d, "Dossier"))).ToArray();

The hasFolder array contains all the drives that contain the folder you are seeking.
 
Share this answer
 
C#
Using System.Text;

StringBuilder sb = new StringBuilder();

(start of loop)
...
sb.AppendLine("xxx exists / does not exist on zzz");
...
(end of loop)

MessageBox.Show( sb.ToString() );
 
Share this answer
 
C#
string[] drives = Directory.GetLogicalDrives().Where(d => Directory.Exists(Path.Combine(d, "Dossier"))).ToArray();
 string Résult = string.Join(", ", drives);
 if (Résult != "")
 {
     MessageBox.Show("exists");
 }
 else
 {
     MessageBox.Show("does not exist");
 }

Excellent, but can we do better!
thank you
 
Share this answer
 

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