public bool TryCreateDirectoryWithAdminAndUserModifyPermissions(string directoryPath, out string error) { try { DirectorySecurity directorySecurity = new DirectorySecurity(); // Protect the access rules from inheritance and remove any inherited rules directorySecurity.SetAccessRuleProtection(isProtected: true, preserveInheritance: false); // Add new ACLs for users and admins. Users will be granted write permissions. AddUsersAccessRulesToDirectorySecurity(directorySecurity, grantUsersModifyPermissions: true); AddAdminAccessRulesToDirectorySecurity(directorySecurity); DirectoryEx.CreateDirectory(directoryPath, directorySecurity); } catch (Exception e) when(e is IOException || e is UnauthorizedAccessException || e is PathTooLongException || e is DirectoryNotFoundException) { error = $"Exception while creating directory `{directoryPath}`: {e.Message}"; return(false); } error = null; return(true); }
private InstanceManager() { // Find the exe path ExeFilePath = Assembly.GetExecutingAssembly().Location; ExeDir = ExeFilePath.SubstringRange(0, ExeFilePath.LastIndexOf('\\') + 1); // Check if this is an installed copy of Sky Jukebox var key = Registry.LocalMachine.OpenSubKey(@"Software\OronDF343\SkyJukebox"); if (key != null) { Console.WriteLine("Installation detected"); var loc = key.GetValue("InstallLocation"); if (((string)loc).Trim('\"').Equals(ExeDir)) { Console.WriteLine("Loading from AppData"); UserDataDir = PathEx.Combine(new DirectoryInfoEx(KnownFolderIds.LocalAppData).FullName, "SkyJukebox"); if (!DirectoryEx.Exists(UserDataDir)) { DirectoryEx.CreateDirectory(UserDataDir); } return; } } UserDataDir = ExeDir; }
/// <summary> /// Gets the location of the default log file repository /// </summary> /// <returns>Returns the path to the log file repository</returns> public static string GetLocation() { var directory = Path.Combine(DirectoryEx.GetCurrentDirectory(), "Log"); if (!Directory.Exists(directory)) { DirectoryEx.CreateDirectory(directory, true, false); } return(directory); }
private void CreateAndConfigureProgramDataDirectories() { string serviceDataRootPath = ScalarPlatform.Instance.GetSecureDataRootForScalar(); // Create Scalar.Service and Scalar.Upgrade related directories (if they don't already exist) // TODO #136: Determine if we still should be creating Scalar.Service here DirectoryEx.CreateDirectory(serviceDataRootPath); DirectoryEx.CreateDirectory(this.serviceDataLocation); DirectoryEx.CreateDirectory(ProductUpgraderInfo.GetUpgradeProtectedDataDirectory()); // Special rules for the upgrader logs and registry, as non-elevated users need to be be able to write this.CreateAndConfigureUserWriteableDirectory(this.repoRegistryLocation); this.CreateAndConfigureUserWriteableDirectory(ProductUpgraderInfo.GetLogDirectoryPath()); this.CreateAndConfigureUserWriteableDirectory(ScalarPlatform.Instance.GetLogsDirectoryForGVFSComponent(ScalarConstants.Service.UIName)); }
public bool TryCreateOrUpdateDirectoryToAdminModifyPermissions(ITracer tracer, string directoryPath, out string error) { try { DirectorySecurity directorySecurity; if (Directory.Exists(directoryPath)) { directorySecurity = DirectoryEx.GetAccessControl(directoryPath); } else { directorySecurity = new DirectorySecurity(); } // Protect the access rules from inheritance and remove any inherited rules directorySecurity.SetAccessRuleProtection(isProtected: true, preserveInheritance: false); // Remove any existing ACLs and add new ACLs for users and admins RemoveAllFileSystemAccessRulesFromDirectorySecurity(directorySecurity); AddUsersAccessRulesToDirectorySecurity(directorySecurity, grantUsersModifyPermissions: false); AddAdminAccessRulesToDirectorySecurity(directorySecurity); DirectoryEx.CreateDirectory(directoryPath, directorySecurity); // Ensure the ACLs are set correctly if the directory already existed DirectoryEx.SetAccessControl(directoryPath, directorySecurity); } catch (Exception e) when(e is IOException || e is SystemException) { EventMetadata metadata = new EventMetadata(); metadata.Add("Exception", e.ToString()); tracer.RelatedError(metadata, $"{nameof(this.TryCreateOrUpdateDirectoryToAdminModifyPermissions)}: Exception while creating/configuring directory"); error = e.Message; return(false); } error = null; return(true); }
private void App_Startup(object sender, StartupEventArgs e) { // Load skins: if (!DirectoryEx.Exists(InstanceManager.Instance.SkinsFolderPath)) { DirectoryEx.CreateDirectory(InstanceManager.Instance.SkinsFolderPath); } SkinManager.Instance.LoadAllSkins(InstanceManager.Instance.SkinsFolderPath); // Load settings: SettingsManager.Init(InstanceManager.Instance.SettingsFilePath); // Set skin: if (!IconManager.Instance.LoadFromSkin((string)SettingsManager.Instance["SelectedSkin"].Value)) { MessageBox.Show("Failed to load skin: " + SettingsManager.Instance["SelectedSkin"].Value, "Error", MessageBoxButton.OK, MessageBoxImage.Asterisk); SettingsManager.Instance["SelectedSkin"].ResetValue(); if (!IconManager.Instance.LoadFromSkin((string)SettingsManager.Instance["SelectedSkin"].Value)) { MessageBox.Show("Failed to load fallback default skin!", "This is a bug!", MessageBoxButton.OK, MessageBoxImage.Asterisk); } } // Load key bindings: KeyBindingManager.Init(InstanceManager.Instance.KeyConfigFilePath); // KeyBindingManager.Instance.Disable = !(bool)SettingsManager.Instance["EnableGlobalKeyBindings"].Value; SettingsManager.Instance["EnableGlobalKeyBindings"].PropertyChanged += (s, args) => KeyBindingManager.Instance.Disable = !(bool)SettingsManager.Instance["EnableGlobalKeyBindings"].Value; // Load plugins: // VERY IMPORTANT: Force evaluation of IEnumerable InstanceManager.Instance.LoadedExtensions = ExtensionLoader.GetCompatibleExtensions <IExtension>(Lib.PathStringUtils.GetExePath()).ToList(); _extAccess = new ExtensionAccess(); foreach (var ex in InstanceManager.Instance.LoadedExtensions) { ex.Instance.Init(_extAccess); } }