Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching an Array?

Status
Not open for further replies.

Kenny100

Technical User
Feb 6, 2001
72
NZ
Hi Folks

Bit new at this C# game so be nice ...

I'd like my piece of code to keep searching for the F:\ drive on my PC, and if it finds it, perform an action. I've tried using the GetLogicalDrives() command and was able to assign all logical drive letters to an array as follows:

string[] drives = System.IO.Directory.GetLogicalDrives();

That seems a logical idea to me. I've got a bit stuck now. How can I search the array for the F:\ drive or am I completely wrong here?!

Cheers,
Kenny.
 
Try something like this:
Code:
int i;
boolean bFound = false;

for (i = 0; i < drives.length; i++) 
{
   if (drives[i] == &quot;F:\\&quot;) {
      // Found it!
      bFound = true;
      break;
   }
}
if (bFound) {
   // Do something useful
}

You could use the foreach operator to iterate through the array, which is cool, but it won't tell you which element of the array contains drive letter F.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top