-
Notifications
You must be signed in to change notification settings - Fork 5k
win32: add fallback to environment vars for system folder #109673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9a5ba6e
win32: add fallback to environment vars for system folder
kasperk81 3d347fe
feedback
kasperk81 8c2909e
Update src/libraries/System.Private.CoreLib/src/System/Environment.Wi…
kasperk81 0c735d6
Merge branch 'main' into patch-3
kasperk81 2cb73ea
Merge branch 'main' into patch-3
kasperk81 03f1500
Merge branch 'main' into patch-3
tannergooding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,7 +217,7 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio | |
// The only SpecialFolderOption defines we have are equivalent to KnownFolderFlags. | ||
|
||
string folderGuid; | ||
|
||
string? fallbackEnv = null; | ||
switch (folder) | ||
{ | ||
// Special-cased values to not use SHGetFolderPath when we have a more direct option available. | ||
|
@@ -230,12 +230,15 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio | |
// Map the SpecialFolder to the appropriate Guid | ||
case SpecialFolder.ApplicationData: | ||
folderGuid = Interop.Shell32.KnownFolders.RoamingAppData; | ||
fallbackEnv = "APPDATA"; | ||
break; | ||
case SpecialFolder.CommonApplicationData: | ||
folderGuid = Interop.Shell32.KnownFolders.ProgramData; | ||
fallbackEnv = "ProgramData"; | ||
break; | ||
case SpecialFolder.LocalApplicationData: | ||
folderGuid = Interop.Shell32.KnownFolders.LocalAppData; | ||
fallbackEnv = "LOCALAPPDATA"; | ||
break; | ||
case SpecialFolder.Cookies: | ||
folderGuid = Interop.Shell32.KnownFolders.Cookies; | ||
|
@@ -292,9 +295,11 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio | |
break; | ||
case SpecialFolder.ProgramFiles: | ||
folderGuid = Interop.Shell32.KnownFolders.ProgramFiles; | ||
fallbackEnv = "ProgramFiles"; | ||
break; | ||
case SpecialFolder.CommonProgramFiles: | ||
folderGuid = Interop.Shell32.KnownFolders.ProgramFilesCommon; | ||
fallbackEnv = "CommonProgramFiles"; | ||
break; | ||
case SpecialFolder.AdminTools: | ||
folderGuid = Interop.Shell32.KnownFolders.AdminTools; | ||
|
@@ -346,12 +351,15 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio | |
break; | ||
case SpecialFolder.UserProfile: | ||
folderGuid = Interop.Shell32.KnownFolders.Profile; | ||
fallbackEnv = "USERPROFILE"; | ||
break; | ||
case SpecialFolder.CommonProgramFilesX86: | ||
folderGuid = Interop.Shell32.KnownFolders.ProgramFilesCommonX86; | ||
fallbackEnv = "CommonProgramFiles(x86)"; | ||
break; | ||
case SpecialFolder.ProgramFilesX86: | ||
folderGuid = Interop.Shell32.KnownFolders.ProgramFilesX86; | ||
fallbackEnv = "ProgramFiles(x86)"; | ||
break; | ||
case SpecialFolder.Resources: | ||
folderGuid = Interop.Shell32.KnownFolders.ResourceDir; | ||
|
@@ -364,18 +372,17 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio | |
break; | ||
case SpecialFolder.Windows: | ||
folderGuid = Interop.Shell32.KnownFolders.Windows; | ||
fallbackEnv = "windir"; | ||
break; | ||
} | ||
|
||
Guid folderId = new Guid(folderGuid); | ||
|
||
int hr = Interop.Shell32.SHGetKnownFolderPath(folderId, (uint)option, IntPtr.Zero, out string path); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is part of the shell experience, so desktop only. So I'm not surprised that it fails on systems without shell. This will probably also fix usages in system services, etc. And probably everything else where no user registry hives are loaded. |
||
if (hr != 0) // Not S_OK | ||
{ | ||
return string.Empty; | ||
} | ||
if (hr == 0) | ||
return path; | ||
|
||
return path; | ||
// Fallback logic if SHGetKnownFolderPath failed (nanoserver) | ||
return fallbackEnv != null ? Environment.GetEnvironmentVariable(fallbackEnv) ?? string.Empty : string.Empty; | ||
} | ||
|
||
// Separate type so a .cctor is not created for Environment which then would be triggered during startup | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ViktorHofer @akoeplinger I removed this condition, and it's now passing where it was failing before: link to comment. I didn’t update other environment tests since they compare values against win32
SHGetFolderPathW
, which returns a general error (-2147467259) on nano server