@@ -217,7 +217,7 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio
217
217
// The only SpecialFolderOption defines we have are equivalent to KnownFolderFlags.
218
218
219
219
string folderGuid ;
220
-
220
+ string ? fallbackEnv = null ;
221
221
switch ( folder )
222
222
{
223
223
// 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
230
230
// Map the SpecialFolder to the appropriate Guid
231
231
case SpecialFolder . ApplicationData :
232
232
folderGuid = Interop . Shell32 . KnownFolders . RoamingAppData ;
233
+ fallbackEnv = "APPDATA" ;
233
234
break ;
234
235
case SpecialFolder . CommonApplicationData :
235
236
folderGuid = Interop . Shell32 . KnownFolders . ProgramData ;
237
+ fallbackEnv = "ProgramData" ;
236
238
break ;
237
239
case SpecialFolder . LocalApplicationData :
238
240
folderGuid = Interop . Shell32 . KnownFolders . LocalAppData ;
241
+ fallbackEnv = "LOCALAPPDATA" ;
239
242
break ;
240
243
case SpecialFolder . Cookies :
241
244
folderGuid = Interop . Shell32 . KnownFolders . Cookies ;
@@ -292,9 +295,11 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio
292
295
break ;
293
296
case SpecialFolder . ProgramFiles :
294
297
folderGuid = Interop . Shell32 . KnownFolders . ProgramFiles ;
298
+ fallbackEnv = "ProgramFiles" ;
295
299
break ;
296
300
case SpecialFolder . CommonProgramFiles :
297
301
folderGuid = Interop . Shell32 . KnownFolders . ProgramFilesCommon ;
302
+ fallbackEnv = "CommonProgramFiles" ;
298
303
break ;
299
304
case SpecialFolder . AdminTools :
300
305
folderGuid = Interop . Shell32 . KnownFolders . AdminTools ;
@@ -346,12 +351,15 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio
346
351
break ;
347
352
case SpecialFolder . UserProfile :
348
353
folderGuid = Interop . Shell32 . KnownFolders . Profile ;
354
+ fallbackEnv = "USERPROFILE" ;
349
355
break ;
350
356
case SpecialFolder . CommonProgramFilesX86 :
351
357
folderGuid = Interop . Shell32 . KnownFolders . ProgramFilesCommonX86 ;
358
+ fallbackEnv = "CommonProgramFiles(x86)" ;
352
359
break ;
353
360
case SpecialFolder . ProgramFilesX86 :
354
361
folderGuid = Interop . Shell32 . KnownFolders . ProgramFilesX86 ;
362
+ fallbackEnv = "ProgramFiles(x86)" ;
355
363
break ;
356
364
case SpecialFolder . Resources :
357
365
folderGuid = Interop . Shell32 . KnownFolders . ResourceDir ;
@@ -364,18 +372,17 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio
364
372
break ;
365
373
case SpecialFolder . Windows :
366
374
folderGuid = Interop . Shell32 . KnownFolders . Windows ;
375
+ fallbackEnv = "windir" ;
367
376
break ;
368
377
}
369
378
370
379
Guid folderId = new Guid ( folderGuid ) ;
371
-
372
380
int hr = Interop . Shell32 . SHGetKnownFolderPath ( folderId , ( uint ) option , IntPtr . Zero , out string path ) ;
373
- if ( hr != 0 ) // Not S_OK
374
- {
375
- return string . Empty ;
376
- }
381
+ if ( hr == 0 )
382
+ return path ;
377
383
378
- return path ;
384
+ // Fallback logic if SHGetKnownFolderPath failed (nanoserver)
385
+ return fallbackEnv != null ? Environment . GetEnvironmentVariable ( fallbackEnv ) ?? string . Empty : string . Empty ;
379
386
}
380
387
381
388
// Separate type so a .cctor is not created for Environment which then would be triggered during startup
0 commit comments