Skip to content

Commit

Permalink
Use Environment.SystemDirectory from GetFolderPath(System) on Windows (
Browse files Browse the repository at this point in the history
…#83593)

* Use Environment.SystemDirectory from GetFolderPath(System) on Windows

* Address PR feedback and fix up a few other occurrences

* Remove KnownFolders.System
  • Loading branch information
stephentoub authored Mar 18, 2023
1 parent 281c462 commit 8cacc0f
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,6 @@ internal static class KnownFolders
/// </summary>
internal const string Startup = "{B97D20BB-F46A-4C97-BA10-5E3608430854}";

/// <summary>
/// (CSIDL_SYSTEM) System32 folder
/// "%windir%\system32"
/// </summary>
internal const string System = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}";

/// <summary>
/// (CSIDL_SYSTEMX86) X86 System32 folder
/// "%windir%\system32" or "%windir%\syswow64"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ internal static void Constructor_InvalidFileNameAsCodeBaseArgument_ShouldThrowIO

internal static void Constructor_DirectoryAsCodeBaseArgument_ShouldThrowFileLoad(Func<string, AssemblyCatalog> catalogCreator)
{
string directory = Environment.GetFolderPath(Environment.SpecialFolder.System);
string directory = Environment.SystemDirectory;
Assert.True(Directory.Exists(directory));

Assert.Throws<FileLoadException>(() => catalogCreator(directory));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio

switch (folder)
{
// Special-cased values to not use SHGetFolderPath when we have a more direct option available.
case SpecialFolder.System:
// This assumes the system directory always exists and thus we don't need to do anything special for any SpecialFolderOption.
return SystemDirectory;
default:
return string.Empty;

// Map the SpecialFolder to the appropriate Guid
case SpecialFolder.ApplicationData:
folderGuid = Interop.Shell32.KnownFolders.RoamingAppData;
break;
Expand Down Expand Up @@ -271,9 +279,6 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio
case SpecialFolder.Startup:
folderGuid = Interop.Shell32.KnownFolders.Startup;
break;
case SpecialFolder.System:
folderGuid = Interop.Shell32.KnownFolders.System;
break;
case SpecialFolder.Templates:
folderGuid = Interop.Shell32.KnownFolders.Templates;
break;
Expand Down Expand Up @@ -360,15 +365,8 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio
case SpecialFolder.Windows:
folderGuid = Interop.Shell32.KnownFolders.Windows;
break;
default:
return string.Empty;
}

return GetKnownFolderPath(folderGuid, option);
}

private static string GetKnownFolderPath(string folderGuid, SpecialFolderOption option)
{
Guid folderId = new Guid(folderGuid);

int hr = Interop.Shell32.SHGetKnownFolderPath(folderId, (uint)option, IntPtr.Zero, out string path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void SubStream()
[Fact]
public void OpenNativeImage()
{
using (var reader = new PEReader(File.OpenRead(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "kernel32.dll"))))
using (var reader = new PEReader(File.OpenRead(Path.Combine(Environment.SystemDirectory, "kernel32.dll"))))
{
Assert.False(reader.HasMetadata);
Assert.True(reader.PEHeaders.IsDll);
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Reflection/tests/AssemblyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ public void LoadFile_PartiallyQualifiedPath_ThrowsArgumentException()
[PlatformSpecific(TestPlatforms.Windows)]
public void LoadFile_ValidPEBadIL_ThrowsBadImageFormatExceptionWithPath()
{
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "kernelbase.dll");
string path = Path.Combine(Environment.SystemDirectory, "kernelbase.dll");
if (!File.Exists(path))
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,17 +381,6 @@ public void GetFolderPath_Unix_SpecialFolderDoesNotExist_CreatesSuccessfully(Env
[Fact]
public void GetSystemDirectory()
{
if (PlatformDetection.IsWindowsNanoServer)
{
// https://github.com/dotnet/runtime/issues/21430
// On Windows Nano, ShGetKnownFolderPath currently doesn't give
// the correct result for SystemDirectory.
// Assert that it's wrong, so that if it's fixed, we don't forget to
// enable this test for Nano.
Assert.NotEqual(Environment.GetFolderPath(Environment.SpecialFolder.System), Environment.SystemDirectory);
return;
}

Assert.Equal(Environment.GetFolderPath(Environment.SpecialFolder.System), Environment.SystemDirectory);
}

Expand Down

0 comments on commit 8cacc0f

Please sign in to comment.