public void SetupSubKeyPathAndAccessRights() { var pathArray = SubKey.Split('\\'); RegistryKey baseRegistryKey = BaseRegKeyCurrentUser; //string ssid = WindowsIdentityHelper.GetUniqueSecurityIdForCurrentUser(); WindowsIdentity windowsIdentity = WindowsIdentityHelper.GetWindowsIdentityForCurrentUser(); foreach (string path in pathArray) { RegistryKey sk1 = baseRegistryKey.OpenSubKey(path, RegistryKeyPermissionCheck.ReadWriteSubTree); bool userAccessGranted = true; if (sk1 == null) { sk1 = baseRegistryKey.CreateSubKey(path); } if (sk1 == null) { userAccessGranted = false; } if (userAccessGranted) { continue; } var registrySecurity = new RegistrySecurity(); IdentityReference identityReference = new NTAccount(windowsIdentity.Name); var rule = new RegistryAccessRule(identityReference, RegistryRights.CreateSubKey | RegistryRights.ReadKey | RegistryRights.WriteKey, AccessControlType.Allow); registrySecurity.AddAccessRule(rule); sk1 = baseRegistryKey.OpenSubKey(path, RegistryKeyPermissionCheck.ReadSubTree); sk1?.SetAccessControl(registrySecurity); } }
/// <summary> /// Initializes a new desktop manager and creates one startup desktop containing all currently open windows. /// This desktop is accessible through the <see cref="AbstractWorkspaceManager{TWorkspace,TSession}.CurrentWorkspace" /> property. /// </summary> /// <param name = "settings"> /// Contains settings for how the desktop manager should behave. E.g. which windows to ignore. /// </param> /// <param name = "persistenceProvider">Allows state of applications to be persisted and restored.</param> /// <exception cref = "BadImageFormatException">Thrown when other than x64 virtual desktop manager is run to operate on x64 platform.</exception> /// <exception cref = "NotSupportedException">Thrown when virtual desktop manager is started without necessary privileges.</exception> public VirtualDesktopManager(ISettings settings, AbstractPersistenceProvider persistenceProvider) { Contract.Requires(settings != null); _persistenceProvider = persistenceProvider; if (!settings.IgnoreRequireElevatedPrivileges) { // ReSharper disable once AssignNullToNotNullAttribute, WindowsIdentity.GetCurrent() will never return null (http://stackoverflow.com/a/15998940/590790) var myPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); if (!myPrincipal.IsInRole(WindowsBuiltInRole.Administrator) && WindowsIdentityHelper.IsUserInAdminGroup()) { throw new NotSupportedException( "The virtual desktop manager should be started with elevated privileges, otherwise it can't manage windows of processes with elevated privileges. " + "Alternatively, set 'IgnoreRequireElevantedPrivileges' to true in the settings passed to the VDM initialization, in which case these windows will be ignored entirely. " + "While debugging, we recommend running Visual Studio with elevated privileges as well."); } } if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess) { throw new BadImageFormatException("A 64 bit version is needed for the Virtual Desktop Manager in order to run on a 64 bit platform."); } // Determine which windows shouldn't be managed by the desktop manager. _windowFilter = settings.CreateWindowFilter(); _hideBehavior = settings.CreateHideBehavior(); // Initialize visible startup desktop. var startupDesktop = new VirtualDesktop(_persistenceProvider) { IsStartupDesktop = true }; SetStartupWorkspace(startupDesktop); startupDesktop.UnresponsiveWindowDetected += OnUnresponsiveWindowDetected; startupDesktop.AddWindows(GetNewWindows()); _monitorServer = new MonitorVdmPipeServer(this); UpdateWindowAssociations(); }