From 063fd9c8f6ac204eae2d48bbfbce2244c2a4b1da Mon Sep 17 00:00:00 2001 From: Mike Richmond Date: Tue, 9 Aug 2016 14:46:22 -0700 Subject: [PATCH] Removing directory creation from initialization. --- .../engine/PropertyAccessor.cs | 41 ++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/src/System.Management.Automation/engine/PropertyAccessor.cs b/src/System.Management.Automation/engine/PropertyAccessor.cs index 02e67aa79..076e4b485 100644 --- a/src/System.Management.Automation/engine/PropertyAccessor.cs +++ b/src/System.Management.Automation/engine/PropertyAccessor.cs @@ -134,7 +134,6 @@ namespace System.Management.Automation { private string psHomeConfigDirectory; private string appDataConfigDirectory; - private const string configDirectoryName = "Configuration"; private const string configFileName = "PowerShellProperties.json"; /// @@ -156,6 +155,38 @@ namespace System.Management.Automation // Sets the per-user configuration directory // appDataConfigDirectory = Utils.GetUserSettingsDirectory(); + if (!Directory.Exists(appDataConfigDirectory)) + { + try + { + Directory.CreateDirectory(appDataConfigDirectory); + } + catch (UnauthorizedAccessException) + { + // Do nothing now. This failure shouldn't block initialization + appDataConfigDirectory = null; + } + } + } + + /// + /// Enables delayed creation of the user settings directory so it does not interfere with PowerShell initialization + /// + /// Returns the directory if present or creatable. Throws otherwise. + private string GetAppDataConfigDirectory() + { + if (null == appDataConfigDirectory) + { + string tempAppDataConfigDir = Utils.GetUserSettingsDirectory(); + if (!Directory.Exists(tempAppDataConfigDir)) + { + Directory.CreateDirectory(tempAppDataConfigDir); + // Only assign it if creation succeeds. It will throw if it fails. + appDataConfigDirectory = tempAppDataConfigDir; + } + // Do not catch exceptions here. Let them flow up. + } + return appDataConfigDirectory; } /// @@ -170,7 +201,7 @@ namespace System.Management.Automation // Defaults to system wide. if (PropertyScope.CurrentUser == scope) { - scopeDirectory = appDataConfigDirectory; + scopeDirectory = GetAppDataConfigDirectory(); } string fileName = Path.Combine(scopeDirectory, configFileName); @@ -205,7 +236,7 @@ namespace System.Management.Automation // Defaults to system wide. if(PropertyScope.CurrentUser == scope) { - scopeDirectory = appDataConfigDirectory; + scopeDirectory = GetAppDataConfigDirectory(); } string fileName = Path.Combine(scopeDirectory, configFileName); @@ -226,7 +257,7 @@ namespace System.Management.Automation // Defaults to system wide. if (PropertyScope.CurrentUser == scope) { - scopeDirectory = appDataConfigDirectory; + scopeDirectory = GetAppDataConfigDirectory(); } string fileName = Path.Combine(scopeDirectory, configFileName); @@ -241,7 +272,7 @@ namespace System.Management.Automation // Defaults to system wide. if (PropertyScope.CurrentUser == scope) { - scopeDirectory = appDataConfigDirectory; + scopeDirectory = GetAppDataConfigDirectory(); } string fileName = Path.Combine(scopeDirectory, configFileName);