The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,565 other followers

.NET/C#: System.Environment.GetLogicalDrives Method is identical to System.IO.Directory.GetLogicalDrives

Posted by jpluimers on 2013/12/26

My mental association with getting LogicalDrives was always the System.IO namespace, so I’ve always used Directory.GetLogicalDrives Method (System.IO) .

Recently I bumped into Environment.GetLogicalDrives Method (System), and discovered it has been available for the same time: since .NET 1.x.

I was not so much amazed that these methods return exactly the same data, but that they have identical code. Not just a single call to some common code: their code is the same, line by line. In .NET 4, they have the code below.

But you can also look at the original .NET 1.x Rotor code here:

Truly amazing (:

–jeroen

via:

[SecuritySafeCritical]
public static string[] GetLogicalDrives()
{
    new EnvironmentPermission(PermissionState.Unrestricted).Demand();
    int logicalDrives = Win32Native.GetLogicalDrives();
    if (logicalDrives == 0)
    {
        __Error.WinIOError();
    }
    uint num2 = (uint) logicalDrives;
    int num3 = 0;
    while (num2 != 0)
    {
        if ((num2 & 1) != 0)
        {
            num3++;
        }
        num2 = num2 >> 1;
    }
    string[] strArray = new string[num3];
    char[] chArray = new char[] { 'A', ':', '\\' };
    num2 = (uint) logicalDrives;
    num3 = 0;
    while (num2 != 0)
    {
        if ((num2 & 1) != 0)
        {
            strArray[num3++] = new string(chArray);
        }
        num2 = num2 >> 1;
        chArray[0] = (char) (chArray[0] + '\x0001');
    }
    return strArray;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: