Add GetLogicalDrives.
authorJeremy Kuhne <jeremy.kuhne@microsoft.com>
Tue, 28 Jun 2016 21:47:08 +0000 (14:47 -0700)
committerJeremy Kuhne <jeremy.kuhne@microsoft.com>
Tue, 28 Jun 2016 22:03:10 +0000 (15:03 -0700)
Commit migrated from https://github.com/dotnet/corefx/commit/3032cd204abd1f07058af805c5e57726eecfd621

src/libraries/System.IO.FileSystem/ref/System.IO.FileSystem.cs
src/libraries/System.IO.FileSystem/src/System.IO.FileSystem.csproj
src/libraries/System.IO.FileSystem/src/System/IO/Directory.Unix.cs [new file with mode: 0644]
src/libraries/System.IO.FileSystem/src/System/IO/Directory.Win32.cs [new file with mode: 0644]
src/libraries/System.IO.FileSystem/src/System/IO/Directory.WinRT.cs [new file with mode: 0644]
src/libraries/System.IO.FileSystem/src/System/IO/Directory.cs
src/libraries/System.IO.FileSystem/tests/Directory/GetLogicalDrives.cs [new file with mode: 0644]
src/libraries/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj

index 39d6881..3def4df 100644 (file)
@@ -50,6 +50,7 @@ namespace System.IO
         public static System.DateTime GetLastAccessTimeUtc(string path) { return default(System.DateTime); }
         public static System.DateTime GetLastWriteTime(string path) { return default(System.DateTime); }
         public static System.DateTime GetLastWriteTimeUtc(string path) { return default(System.DateTime); }
+        public static string[] GetLogicalDrives() { return default(string[]); }
         public static System.IO.DirectoryInfo GetParent(string path) { return default(System.IO.DirectoryInfo); }
         public static void Move(string sourceDirName, string destDirName) { }
         public static void SetCreationTime(string path, System.DateTime creationTime) { }
index aacf4be..5d2bed6 100644 (file)
     <Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.CopyFile.cs">
       <Link>Common\Interop\Windows\Interop.CopyFile.cs</Link>
     </Compile>
+    <Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.GetLogicalDrive.cs">
+      <Link>Common\Interop\Windows\Interop.GetLogicalDrive.cs</Link>
+    </Compile>
+    <Compile Include="System\IO\Directory.Win32.cs" />
     <Compile Include="System\IO\FileSystem.Current.Win32.cs" />
     <Compile Include="System\IO\FileSystemInfo.Win32.cs" />
     <Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.CreateFile.cs">
     <Compile Include="$(CommonPath)\Interop\Windows\mincore\WinRT\Interop.SetErrorMode.cs">
       <Link>Common\Interop\Windows\WinRT\Interop.SetErrorMode.cs</Link>
     </Compile>
+    <Compile Include="System\IO\Directory.WinRT.cs" />
     <Compile Include="System\IO\FileSystem.Current.MuxWin32WinRT.cs" />
     <Compile Include="System\IO\FileSystemInfo.WinRT.cs" />
     <Compile Include="System\IO\MultiplexingWin32WinRTFileSystem.cs" />
   <!-- Unix -->
   <ItemGroup Condition="'$(TargetsUnix)' == 'true'">
     <Compile Include="Microsoft\Win32\SafeHandles\SafeFileHandle.Unix.cs" />
+    <Compile Include="System\IO\Directory.Unix.cs" />
     <Compile Include="System\IO\FileStream.Unix.cs" />
     <Compile Include="System\IO\FileSystem.Current.Unix.cs" />
     <Compile Include="System\IO\FileSystemInfo.Unix.cs" />
diff --git a/src/libraries/System.IO.FileSystem/src/System/IO/Directory.Unix.cs b/src/libraries/System.IO.FileSystem/src/System/IO/Directory.Unix.cs
new file mode 100644 (file)
index 0000000..f387838
--- /dev/null
@@ -0,0 +1,14 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+namespace System.IO
+{
+    public static partial class Directory
+    {
+        public static string[] GetLogicalDrives()
+        {
+            throw new PlatformNotSupportedException();
+        }
+    }
+}
diff --git a/src/libraries/System.IO.FileSystem/src/System/IO/Directory.Win32.cs b/src/libraries/System.IO.FileSystem/src/System/IO/Directory.Win32.cs
new file mode 100644 (file)
index 0000000..0cbede0
--- /dev/null
@@ -0,0 +1,38 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+namespace System.IO
+{
+    public static partial class Directory
+    {
+        public static string[] GetLogicalDrives()
+        {
+            int drives = Interop.mincore.GetLogicalDrives();
+            if (drives == 0)
+                throw Win32Marshal.GetExceptionForLastWin32Error();
+
+            uint d = (uint)drives;
+            int count = 0;
+            while (d != 0)
+            {
+                if (((int)d & 1) != 0) count++;
+                d >>= 1;
+            }
+            string[] result = new string[count];
+            char[] root = new char[] { 'A', ':', '\\' };
+            d = (uint)drives;
+            count = 0;
+            while (d != 0)
+            {
+                if (((int)d & 1) != 0)
+                {
+                    result[count++] = new string(root);
+                }
+                d >>= 1;
+                root[0]++;
+            }
+            return result;
+        }
+    }
+}
diff --git a/src/libraries/System.IO.FileSystem/src/System/IO/Directory.WinRT.cs b/src/libraries/System.IO.FileSystem/src/System/IO/Directory.WinRT.cs
new file mode 100644 (file)
index 0000000..f387838
--- /dev/null
@@ -0,0 +1,14 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+namespace System.IO
+{
+    public static partial class Directory
+    {
+        public static string[] GetLogicalDrives()
+        {
+            throw new PlatformNotSupportedException();
+        }
+    }
+}
index bb15f39..2eac0f1 100644 (file)
@@ -17,7 +17,7 @@ using System.Threading;
 
 namespace System.IO
 {
-    public static class Directory
+    public static partial class Directory
     {
         public static DirectoryInfo GetParent(String path)
         {
diff --git a/src/libraries/System.IO.FileSystem/tests/Directory/GetLogicalDrives.cs b/src/libraries/System.IO.FileSystem/tests/Directory/GetLogicalDrives.cs
new file mode 100644 (file)
index 0000000..67eeab6
--- /dev/null
@@ -0,0 +1,29 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System.Linq;
+using System.Runtime.InteropServices;
+using Xunit;
+
+namespace System.IO.Tests
+{
+    public class Directory_GetLogicalDrives
+    {
+        [Fact]
+        [PlatformSpecific(PlatformID.AnyUnix)]
+        public void ThrowsPlatformNotSupported_Unix()
+        {
+            Assert.Throws<PlatformNotSupportedException>(() => Directory.GetLogicalDrives());
+        }
+
+        [Fact]
+        [PlatformSpecific(PlatformID.Windows)]
+        public void GetsValidDriveStrings_Windows()
+        {
+            string[] drives = Directory.GetLogicalDrives();
+            Assert.NotEmpty(drives);
+            Assert.All(drives, d => Assert.Matches(@"^[A-Z]:\\$", d));
+        }
+    }
+}
index 2412b83..b5534bc 100644 (file)
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
   <PropertyGroup>
     <Configuration Condition="'$(Configuration)'==''">Windows_Debug</Configuration>
@@ -43,6 +43,7 @@
     <Compile Include="DirectoryInfo\Root.cs" />
     <Compile Include="Directory\EnumerableAPIs.cs" />
     <Compile Include="Directory\GetFileSystemEntries_str_str_so.cs" />
+    <Compile Include="Directory\GetLogicalDrives.cs" />
     <Compile Include="Directory\GetParent.cs" />
     <Compile Include="FileInfo\GetSetAttributes.cs" />
     <Compile Include="FileInfo\Length.cs" />