mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 04:36:39 +00:00
Merge pull request #1694 from PowerShell/mirichmo/fixing-configfileaccessor-constructor
Resolves a JsonConfigFileAccessor constructor exception to fix #1689
This commit is contained in:
commit
1ef400d62b
1 file changed
+38
-16
@@ -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";
|
||||
|
||||
/// <summary>
|
||||
@@ -147,27 +146,50 @@ namespace System.Management.Automation
|
||||
internal JsonConfigFileAccessor()
|
||||
{
|
||||
//
|
||||
// Initialize (and create if necessary) the system-wide configuration directory
|
||||
// Sets the system-wide configuration directory
|
||||
//
|
||||
Assembly assembly = typeof(PSObject).GetTypeInfo().Assembly;
|
||||
psHomeConfigDirectory = Path.Combine(Path.GetDirectoryName(assembly.Location), configDirectoryName);
|
||||
|
||||
if (!Directory.Exists(psHomeConfigDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(psHomeConfigDirectory);
|
||||
}
|
||||
psHomeConfigDirectory = Path.GetDirectoryName(assembly.Location);
|
||||
|
||||
//
|
||||
// Initialize (and create if necessary) the per-user configuration directory
|
||||
// Sets the per-user configuration directory
|
||||
//
|
||||
appDataConfigDirectory = Path.Combine(Utils.GetUserSettingsDirectory(), configDirectoryName);
|
||||
|
||||
appDataConfigDirectory = Utils.GetUserSettingsDirectory();
|
||||
if (!Directory.Exists(appDataConfigDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(appDataConfigDirectory);
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(appDataConfigDirectory);
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
// Do nothing now. This failure shouldn't block initialization
|
||||
appDataConfigDirectory = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enables delayed creation of the user settings directory so it does
|
||||
/// not interfere with PowerShell initialization
|
||||
/// </summary>
|
||||
/// <returns>Returns the directory if present or creatable. Throws otherwise.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This value is not writable via the API and must be set using a text editor.
|
||||
/// </summary>
|
||||
@@ -180,7 +202,7 @@ namespace System.Management.Automation
|
||||
// Defaults to system wide.
|
||||
if (PropertyScope.CurrentUser == scope)
|
||||
{
|
||||
scopeDirectory = appDataConfigDirectory;
|
||||
scopeDirectory = GetAppDataConfigDirectory();
|
||||
}
|
||||
|
||||
string fileName = Path.Combine(scopeDirectory, configFileName);
|
||||
@@ -215,7 +237,7 @@ namespace System.Management.Automation
|
||||
// Defaults to system wide.
|
||||
if(PropertyScope.CurrentUser == scope)
|
||||
{
|
||||
scopeDirectory = appDataConfigDirectory;
|
||||
scopeDirectory = GetAppDataConfigDirectory();
|
||||
}
|
||||
|
||||
string fileName = Path.Combine(scopeDirectory, configFileName);
|
||||
@@ -236,7 +258,7 @@ namespace System.Management.Automation
|
||||
// Defaults to system wide.
|
||||
if (PropertyScope.CurrentUser == scope)
|
||||
{
|
||||
scopeDirectory = appDataConfigDirectory;
|
||||
scopeDirectory = GetAppDataConfigDirectory();
|
||||
}
|
||||
|
||||
string fileName = Path.Combine(scopeDirectory, configFileName);
|
||||
@@ -251,7 +273,7 @@ namespace System.Management.Automation
|
||||
// Defaults to system wide.
|
||||
if (PropertyScope.CurrentUser == scope)
|
||||
{
|
||||
scopeDirectory = appDataConfigDirectory;
|
||||
scopeDirectory = GetAppDataConfigDirectory();
|
||||
}
|
||||
|
||||
string fileName = Path.Combine(scopeDirectory, configFileName);
|
||||
|
||||
Reference in new issue
Block a user