.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:
- File: System.IO.Directory – 123aspx.com ASP.NET Resource Directory.
- File: System.Environment – 123aspx.com ASP.NET Resource Directory.
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 comment