@@ -112,8 +112,9 @@ public void ForSystemRun_DocumentsReadOnly_AppearsInReadonlyPaths()
112112
113113 Assert . NotNull ( policy . Filesystem ! . ReadonlyPaths ) ;
114114 Assert . Empty ( policy . Filesystem . ReadwritePaths ! ) ;
115+ var documentsPath = Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) ;
115116 Assert . Contains ( policy . Filesystem . ReadonlyPaths ! ,
116- p => p . EndsWith ( "Documents" , StringComparison . OrdinalIgnoreCase ) ) ;
117+ p => string . Equals ( p , documentsPath , StringComparison . OrdinalIgnoreCase ) ) ;
117118 }
118119
119120 [ Fact ]
@@ -123,8 +124,9 @@ public void ForSystemRun_DesktopReadWrite_AppearsInReadwritePaths()
123124 var policy = MxcPolicyBuilder . ForSystemRun ( settings , "C:\\ s" ) ;
124125
125126 Assert . NotNull ( policy . Filesystem ! . ReadwritePaths ) ;
127+ var desktopPath = Environment . GetFolderPath ( Environment . SpecialFolder . DesktopDirectory ) ;
126128 Assert . Contains ( policy . Filesystem . ReadwritePaths ! ,
127- p => p . EndsWith ( "Desktop" , StringComparison . OrdinalIgnoreCase ) ) ;
129+ p => string . Equals ( p , desktopPath , StringComparison . OrdinalIgnoreCase ) ) ;
128130 }
129131
130132 [ Fact ]
@@ -133,8 +135,9 @@ public void ForSystemRun_DownloadsReadOnly_AppearsInReadonlyPaths()
133135 var settings = new SettingsData { SandboxDownloadsAccess = SandboxFolderAccess . ReadOnly } ;
134136 var policy = MxcPolicyBuilder . ForSystemRun ( settings , "C:\\ s" ) ;
135137
138+ var downloadsPath = GetExpectedDownloadsPath ( ) ;
136139 Assert . Contains ( policy . Filesystem ! . ReadonlyPaths ! ,
137- p => p . EndsWith ( "Downloads" , StringComparison . OrdinalIgnoreCase ) ) ;
140+ p => string . Equals ( p , downloadsPath , StringComparison . OrdinalIgnoreCase ) ) ;
138141 }
139142
140143 [ Fact ]
@@ -273,4 +276,36 @@ public void ForSystemRun_CustomFolder_NotOverlappingDeny_StillGranted()
273276
274277 Assert . Contains ( "D:\\ code\\ my-project" , policy . Filesystem ! . ReadwritePaths ! ) ;
275278 }
279+
280+ private static string GetExpectedDownloadsPath ( )
281+ {
282+ var userProfile = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ;
283+ return ResolveKnownFolderDownloads ( ) ?? Path . Combine ( userProfile , "Downloads" ) ;
284+ }
285+
286+ private static readonly Guid s_folderIdDownloads =
287+ new ( "374DE290-123F-4565-9164-39C4925E467B" ) ;
288+
289+ private static string ? ResolveKnownFolderDownloads ( )
290+ {
291+ if ( ! OperatingSystem . IsWindows ( ) ) return null ;
292+ try
293+ {
294+ var hr = SHGetKnownFolderPath ( s_folderIdDownloads , 0 , IntPtr . Zero , out var ptr ) ;
295+ if ( hr != 0 || ptr == IntPtr . Zero ) return null ;
296+ try { return System . Runtime . InteropServices . Marshal . PtrToStringUni ( ptr ) ; }
297+ finally { System . Runtime . InteropServices . Marshal . FreeCoTaskMem ( ptr ) ; }
298+ }
299+ catch
300+ {
301+ return null ;
302+ }
303+ }
304+
305+ [ System . Runtime . InteropServices . DllImport ( "shell32.dll" , CharSet = System . Runtime . InteropServices . CharSet . Unicode , ExactSpelling = true ) ]
306+ private static extern int SHGetKnownFolderPath (
307+ [ System . Runtime . InteropServices . MarshalAs ( System . Runtime . InteropServices . UnmanagedType . LPStruct ) ] Guid rfid ,
308+ uint dwFlags ,
309+ IntPtr hToken ,
310+ out IntPtr ppszPath ) ;
276311}
0 commit comments