Skip to content

Commit 7264c87

Browse files
authored
Use Environment.SystemDirectory to get system directory (#47271)
* Use Environment.SystemDirectory to get system directory This reduces startup time by not loading shell32.dll. Fix #47269 * Update commit SHA
1 parent 99a1d2c commit 7264c87

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/DefaultBuilder/src/WebApplicationBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,18 @@ private static void SetDefaultContentRoot(WebApplicationOptions options, Configu
182182
{
183183
if (options.ContentRootPath is null && configuration[HostDefaults.ContentRootKey] is null)
184184
{
185-
// Logic taken from https://github.com/dotnet/runtime/blob/78ed4438a42acab80541e9bde1910abaa8841db2/src/libraries/Microsoft.Extensions.Hosting/src/HostingHostBuilderExtensions.cs#L209-L227
185+
// Logic taken from https://github.com/dotnet/runtime/blob/dc5a6c8be1644915c14c4a464447b0d54e223a46/src/libraries/Microsoft.Extensions.Hosting/src/HostingHostBuilderExtensions.cs#L209-L227
186186

187187
// If we're running anywhere other than C:\Windows\system32, we default to using the CWD for the ContentRoot.
188188
// However, since many things like Windows services and MSIX installers have C:\Windows\system32 as there CWD which is not likely
189189
// to really be the home for things like appsettings.json, we skip changing the ContentRoot in that case. The non-"default" initial
190190
// value for ContentRoot is AppContext.BaseDirectory (e.g. the executable path) which probably makes more sense than the system32.
191191

192-
// In my testing, both Environment.CurrentDirectory and Environment.GetFolderPath(Environment.SpecialFolder.System) return the path without
192+
// In my testing, both Environment.CurrentDirectory and Environment.SystemDirectory return the path without
193193
// any trailing directory separator characters. I'm not even sure the casing can ever be different from these APIs, but I think it makes sense to
194194
// ignore case for Windows path comparisons given the file system is usually (always?) going to be case insensitive for the system path.
195195
string cwd = System.Environment.CurrentDirectory;
196-
if (!OperatingSystem.IsWindows() || !string.Equals(cwd, System.Environment.GetFolderPath(System.Environment.SpecialFolder.System), StringComparison.OrdinalIgnoreCase))
196+
if (!OperatingSystem.IsWindows() || !string.Equals(cwd, System.Environment.SystemDirectory, StringComparison.OrdinalIgnoreCase))
197197
{
198198
configuration.AddInMemoryCollection(new[]
199199
{

src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ public void ContentRootIsDefaultedToCurrentDirectory()
595595
public void ContentRootIsBaseDirectoryWhenCurrentIsSpecialFolderSystem()
596596
{
597597
var options = new RemoteInvokeOptions();
598-
options.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System);
598+
options.StartInfo.WorkingDirectory = Environment.SystemDirectory;
599599

600600
using var remoteHandle = RemoteExecutor.Invoke(static () =>
601601
{

0 commit comments

Comments
 (0)