Current directory code duplicates Environment (dotnet/corefx#27153)
authorJeremy Kuhne <jeremy.kuhne@microsoft.com>
Thu, 15 Feb 2018 18:32:03 +0000 (10:32 -0800)
committerGitHub <noreply@github.com>
Thu, 15 Feb 2018 18:32:03 +0000 (10:32 -0800)
Just call Environment.CurrentDirectory. Remove a number of P/Invokes
that are no longer in use.

Commit migrated from https://github.com/dotnet/corefx/commit/8e6222ab2f2b48f20a274272693665dac8259c79

src/libraries/System.IO.FileSystem/src/System.IO.FileSystem.csproj
src/libraries/System.IO.FileSystem/src/System/IO/Directory.cs
src/libraries/System.IO.FileSystem/src/System/IO/FileSystem.Unix.cs
src/libraries/System.IO.FileSystem/src/System/IO/FileSystem.Windows.cs

index f6ddc7b..e43f169 100644 (file)
     <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.SECURITY_ATTRIBUTES.cs">
       <Link>Common\Interop\Windows\Interop.SECURITY_ATTRIBUTES.cs</Link>
     </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.SecurityOptions.cs">
-      <Link>Common\Interop\Windows\Interop.SecurityOptions.cs</Link>
-    </Compile>
     <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.FileTypes.cs">
       <Link>Common\Interop\Windows\Interop.FileTypes.cs</Link>
     </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.GetFileType_SafeHandle.cs">
-      <Link>Common\Interop\Windows\Interop.GetFileType.cs</Link>
-    </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.FlushFileBuffers.cs">
-      <Link>Common\Interop\Windows\Interop.FlushFileBuffers.cs</Link>
-    </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.SetEndOfFile.cs">
-      <Link>Common\Interop\Windows\Interop.SetEndOfFile.cs</Link>
-    </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.SetFilePointerEx.cs">
-      <Link>Common\Interop\Windows\Interop.SetFilePointerEx.cs</Link>
-    </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.CancelIoEx.cs">
-      <Link>Common\Interop\Windows\Interop.CancelIoEx.cs</Link>
-    </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.ReadFile_SafeHandle_NativeOverlapped.cs">
-      <Link>Common\Interop\Windows\Interop.ReadFile_NativeOverlapped.cs</Link>
-    </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.WriteFile_SafeHandle_NativeOverlapped.cs">
-      <Link>Common\Interop\Windows\Interop.WriteFile_NativeOverlapped.cs</Link>
-    </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs">
-      <Link>Common\Interop\Windows\Interop.ReadFile_IntPtr.cs</Link>
-    </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.WriteFile_SafeHandle_IntPtr.cs">
-      <Link>Common\Interop\Windows\Interop.WriteFile_IntPtr.cs</Link>
-    </Compile>
     <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.SetFileInformationByHandle.cs">
       <Link>Common\Interop\Windows\Interop.SetFileInformationByHandle.cs</Link>
     </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.GetCurrentDirectory.cs">
-      <Link>Common\Interop\Windows\Interop.GetCurrentDirectory.cs</Link>
-    </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.SetCurrentDirectory.cs">
-      <Link>Common\Interop\Windows\Interop.SetCurrentDirectory.cs</Link>
-    </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.GetLongPathName.cs">
-      <Link>Common\Interop\Windows\Interop.GetLongPathName.cs</Link>
-    </Compile>
     <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.FindNextFile.cs">
       <Link>Common\Interop\Windows\Interop.FindNextFile.cs</Link>
     </Compile>
index 1c7fc09..9b3de6b 100644 (file)
@@ -255,10 +255,7 @@ namespace System.IO
             return path.Substring(0, PathInternal.GetRootLength(path));
         }
 
-        public static string GetCurrentDirectory()
-        {
-            return FileSystem.GetCurrentDirectory();
-        }
+        public static string GetCurrentDirectory() => Environment.CurrentDirectory;
 
         public static void SetCurrentDirectory(string path)
         {
@@ -267,9 +264,7 @@ namespace System.IO
             if (path.Length == 0)
                 throw new ArgumentException(SR.Argument_PathEmpty, nameof(path));
 
-            string fulldestDirName = Path.GetFullPath(path);
-
-            FileSystem.SetCurrentDirectory(fulldestDirName);
+            Environment.CurrentDirectory = Path.GetFullPath(path);
         }
 
         public static void Move(string sourceDirName, string destDirName)
index 013c09e..9ec2eee 100644 (file)
@@ -449,16 +449,6 @@ namespace System.IO
             return name == "." || name == "..";
         }
 
-        public static string GetCurrentDirectory()
-        {
-            return Interop.Sys.GetCwd();
-        }
-
-        public static void SetCurrentDirectory(string fullPath)
-        {
-            Interop.CheckIo(Interop.Sys.ChDir(fullPath), fullPath, isDirectory:true);
-        }
-
         public static FileAttributes GetAttributes(string fullPath)
         {
             FileAttributes attributes = new FileInfo(fullPath, null).Attributes;
index 8c2dad2..3e106c3 100644 (file)
@@ -259,36 +259,6 @@ namespace System.IO
             return (FileAttributes)data.dwFileAttributes;
         }
 
-        public static string GetCurrentDirectory()
-        {
-            StringBuilder sb = StringBuilderCache.Acquire(Interop.Kernel32.MAX_PATH + 1);
-            if (Interop.Kernel32.GetCurrentDirectory(sb.Capacity, sb) == 0)
-                throw Win32Marshal.GetExceptionForLastWin32Error();
-            string currentDirectory = sb.ToString();
-            // Note that if we have somehow put our command prompt into short
-            // file name mode (i.e. by running edlin or a DOS grep, etc), then
-            // this will return a short file name.
-            if (currentDirectory.IndexOf('~') >= 0)
-            {
-                int r = Interop.Kernel32.GetLongPathName(currentDirectory, sb, sb.Capacity);
-                if (r == 0 || r >= Interop.Kernel32.MAX_PATH)
-                {
-                    int errorCode = Marshal.GetLastWin32Error();
-                    if (r >= Interop.Kernel32.MAX_PATH)
-                        errorCode = Interop.Errors.ERROR_FILENAME_EXCED_RANGE;
-                    if (errorCode != Interop.Errors.ERROR_FILE_NOT_FOUND &&
-                        errorCode != Interop.Errors.ERROR_PATH_NOT_FOUND &&
-                        errorCode != Interop.Errors.ERROR_INVALID_FUNCTION &&  // by design - enough said.
-                        errorCode != Interop.Errors.ERROR_ACCESS_DENIED)
-                        throw Win32Marshal.GetExceptionForWin32Error(errorCode);
-                }
-                currentDirectory = sb.ToString();
-            }
-            StringBuilderCache.Release(sb);
-
-            return currentDirectory;
-        }
-
         public static DateTimeOffset GetCreationTime(string fullPath)
         {
             Interop.Kernel32.WIN32_FILE_ATTRIBUTE_DATA data = new Interop.Kernel32.WIN32_FILE_ATTRIBUTE_DATA();
@@ -562,20 +532,6 @@ namespace System.IO
             }
         }
 
-        public static void SetCurrentDirectory(string fullPath)
-        {
-            if (!Interop.Kernel32.SetCurrentDirectory(fullPath))
-            {
-                // If path doesn't exist, this sets last error to 2 (File 
-                // not Found).  LEGACY: This may potentially have worked correctly
-                // on Win9x, maybe.
-                int errorCode = Marshal.GetLastWin32Error();
-                if (errorCode == Interop.Errors.ERROR_FILE_NOT_FOUND)
-                    errorCode = Interop.Errors.ERROR_PATH_NOT_FOUND;
-                throw Win32Marshal.GetExceptionForWin32Error(errorCode, fullPath);
-            }
-        }
-
         public static void SetLastAccessTime(string fullPath, DateTimeOffset time, bool asDirectory)
         {
             using (SafeFileHandle handle = OpenHandle(fullPath, asDirectory))