/// <summary> /// Writes the specified <see cref="InsuranceRegistryKey"/> to the registry key represented by the specified <see cref="RegistryKey"/>. /// </summary> /// <param name="insuranceRegistryKey"></param> /// <returns></returns> public static void Write(InsuranceRegistryKey insuranceRegistryKey) { using (var regKey = Registry.CurrentUser.OpenSubKey(insuranceRegistryKey.RegistryKeyName, true)) { if (regKey == null || regKey.GetValueNames().Length != 0) { throw new ArgumentException("The specified InsuranceRegistryKey points to an invalid registry key.", "insuranceRegistryKey"); } regKey.SetValue("guid", insuranceRegistryKey.InsuranceIdentifier.ToString(), RegistryValueKind.String); regKey.SetValue("machineId", insuranceRegistryKey.MachineId); regKey.SetValue("creationDateTime", insuranceRegistryKey.TimeStamp.ToString(_DateTimeFormat)); var i = 0; foreach (var assembly in insuranceRegistryKey.Assemblies) { regKey.SetValue("assembly" + ++i, assembly.ToString(), RegistryValueKind.String); } // Write the InstallerDescription var regKeyInstaller = regKey.CreateSubKey("Installer"); if (regKeyInstaller == null) { throw new Exception(); } regKeyInstaller.SetValue("type", insuranceRegistryKey.InstallerDescription.Type, RegistryValueKind.String); regKeyInstaller.SetValue("id", insuranceRegistryKey.InstallerDescription.Id, RegistryValueKind.String); regKeyInstaller.SetValue("descr", insuranceRegistryKey.InstallerDescription.Description, RegistryValueKind.String); } }
/// <summary> /// Returns the <see cref="CleanUpInsurance"/> matching the identifier specified. /// If no match is found, null is returned. /// </summary> /// <exception cref="ArgumentException"></exception> /// <param name="trackingFilesFolder"></param> /// <param name="trackingRegistryKey"></param> /// <param name="insuranceId"></param> /// <returns></returns> public static CleanUpInsurance LoadFromSystem(string trackingFilesFolder, string trackingRegistryKey, Guid insuranceId) { if (insuranceId == Guid.Empty) { throw new ArgumentException("The specified insurance identifier is not a valid GUID", "insuranceId"); } InsuranceFile insuranceFile = null; InsuranceRegistryKey insuranceRegKey = null; // Load from file if (trackingFilesFolder != null && File.Exists(Path.Combine(trackingFilesFolder, insuranceId.ToString()))) { InsuranceFile.TryRead(Path.Combine(trackingFilesFolder, insuranceId.ToString()), out insuranceFile); } // Load from registry if (trackingRegistryKey != null) { using (var regKey = Registry.CurrentUser.OpenSubKey(trackingRegistryKey + insuranceId, false)) if (regKey != null) // Verify existence of the key { InsuranceRegistryKey.TryRead(regKey, out insuranceRegKey); } } // Possible to check for a running process? // Return null if no CleanUpInsurance can be built from the retrieved data if (insuranceFile == null && insuranceRegKey == null) { return(null); } return(new CleanUpInsurance(insuranceFile, insuranceRegKey, null)); }
private void CreateRegistryInsurance() { using (var rootKey = Registry.CurrentUser.CreateSubKey(_data.TrackingRegistryKey)) rootKey.CreateSubKey(_uniqueId.ToString()).Close(); _insuranceRegistryKey = new InsuranceRegistryKey(Path.Combine(_data.TrackingRegistryKey, _uniqueId.ToString()), _uniqueId, _data.Installer, LocalMachine.Identifier, _timeStamp, _assemblies); InsuranceRegistryKey.Write(_insuranceRegistryKey); }
/// <summary> /// Tries to build an instance of <see cref="InsuranceRegistryKey"/> from data read from the specified registry key. /// </summary> /// <param name="registryKey"></param> /// <param name="insuranceRegistryKey"></param> /// <returns></returns> public static bool TryRead(RegistryKey registryKey, out InsuranceRegistryKey insuranceRegistryKey) { try { insuranceRegistryKey = Read(registryKey); return(true); } catch { insuranceRegistryKey = null; return(false); } }
/// <summary> /// Writes the specified <see cref="InsuranceRegistryKey"/> to the registry key represented by the specified <see cref="RegistryKey"/>. /// </summary> /// <param name="insuranceRegistryKey"></param> /// <returns></returns> public static void Write(InsuranceRegistryKey insuranceRegistryKey) { using (var regKey = Registry.CurrentUser.OpenSubKey(insuranceRegistryKey.RegistryKeyName, true)) { if (regKey == null || regKey.GetValueNames().Length != 0) throw new ArgumentException("The specified InsuranceRegistryKey points to an invalid registry key.", "insuranceRegistryKey"); regKey.SetValue("guid", insuranceRegistryKey.InsuranceIdentifier.ToString(), RegistryValueKind.String); regKey.SetValue("machineId", insuranceRegistryKey.MachineId); regKey.SetValue("creationDateTime", insuranceRegistryKey.TimeStamp.ToString(_DateTimeFormat)); var i = 0; foreach (var assembly in insuranceRegistryKey.Assemblies) regKey.SetValue("assembly" + ++i, assembly.ToString(), RegistryValueKind.String); // Write the InstallerDescription var regKeyInstaller = regKey.CreateSubKey("Installer"); if (regKeyInstaller == null) throw new Exception(); regKeyInstaller.SetValue("type", insuranceRegistryKey.InstallerDescription.Type, RegistryValueKind.String); regKeyInstaller.SetValue("id", insuranceRegistryKey.InstallerDescription.Id, RegistryValueKind.String); regKeyInstaller.SetValue("descr", insuranceRegistryKey.InstallerDescription.Description, RegistryValueKind.String); } }
/// <summary> /// Initializes a new instance of <see cref="CleanUpInsurance"/> built from the data specified. /// </summary> /// <exception cref="ArgumentException"> /// An <see cref="ArgumentException"/> is thrown if <paramref name="insuranceFile"/> and <paramref name="insuranceRegistryKey"/> don't match. /// </exception> /// <exception cref="ArgumentNullException"> /// An <see cref="ArgumentNullException"/> is thrown if both <paramref name="insuranceFile"/> and <paramref name="insuranceRegistryKey"/> are null. /// </exception> /// <param name="insuranceFile"></param> /// <param name="insuranceRegistryKey"></param> /// <param name="insuranceProcess"></param> private CleanUpInsurance(InsuranceFile insuranceFile, InsuranceRegistryKey insuranceRegistryKey, Process insuranceProcess) { _insuranceFile = insuranceFile; _insuranceProcess = insuranceProcess; _insuranceRegistryKey = insuranceRegistryKey; var flags = (_insuranceFile != null ? CleanUpInsuranceFlags.TrackByFile : CleanUpInsuranceFlags.None) | (_insuranceProcess != null ? CleanUpInsuranceFlags.ByWatchService : CleanUpInsuranceFlags.None) | (_insuranceRegistryKey != null ? CleanUpInsuranceFlags.TrackByRegistry : CleanUpInsuranceFlags.None); InsuranceBase insuranceBase = null; if (_insuranceFile != null) { insuranceBase = _insuranceFile; if (_insuranceRegistryKey != null) { if (!insuranceBase.MatchesWith(_insuranceRegistryKey, false)) { throw new ArgumentException("The InsuranceFile and InsuranceRegistryKey don't match.", "insuranceFile"); } insuranceBase.JoinWith(_insuranceRegistryKey); } } else if (_insuranceRegistryKey != null) { insuranceBase = _insuranceRegistryKey; } if (insuranceBase == null) { throw new ArgumentNullException("insuranceFile", "At least one of both insuranceFile and insuranceRegistryKey needs to be initialized."); } _assemblies = new List <AssemblyName>(insuranceBase.Assemblies); _timeStamp = insuranceBase.TimeStamp; _uniqueId = insuranceBase.InsuranceIdentifier; _data = new InsuranceData(insuranceBase.InstallerDescription, flags, _insuranceFile != null ? Path.GetDirectoryName(_insuranceFile.FileName) : null, _insuranceRegistryKey != null ? Path.GetDirectoryName(_insuranceRegistryKey.RegistryKeyName) : null, null); }
/// <summary> /// Returns a list of <see cref="InsuranceRegistryKey"/>s which are read from the registrykey with the specified name. /// </summary> /// <param name="regKeyName">The name of a subkey of the CurrentUser rootkey.</param> /// <returns></returns> private static List <InsuranceRegistryKey> GetRegistryInsurances(string regKeyName) { using (var regKey = Registry.CurrentUser.OpenSubKey(regKeyName)) { if (regKey == null) { return(new List <InsuranceRegistryKey>(0)); } var items = new List <InsuranceRegistryKey>(); var subKeys = regKey.GetSubKeyNames(); foreach (var subKeyName in subKeys) { using (var subKey = regKey.OpenSubKey(subKeyName)) { InsuranceRegistryKey item; if (InsuranceRegistryKey.TryRead(subKey, out item)) { items.Add(item); } } } return(items); } }
/// <summary> /// Tries to build an instance of <see cref="InsuranceRegistryKey"/> from data read from the specified registry key. /// </summary> /// <param name="registryKey"></param> /// <param name="insuranceRegistryKey"></param> /// <returns></returns> public static bool TryRead(RegistryKey registryKey, out InsuranceRegistryKey insuranceRegistryKey) { try { insuranceRegistryKey = Read(registryKey); return true; } catch { insuranceRegistryKey = null; return false; } }
private void CreateRegistryInsurance() { using (var rootKey = Registry.CurrentUser.CreateSubKey(_data.TrackingRegistryKey)) rootKey.CreateSubKey(_uniqueId.ToString()).Close(); _insuranceRegistryKey = new InsuranceRegistryKey(Path.Combine(_data.TrackingRegistryKey, _uniqueId.ToString()), _uniqueId, _data.Installer, LocalMachine.Identifier, _timeStamp, _assemblies); InsuranceRegistryKey.Write(_insuranceRegistryKey); }
/// <summary> /// Initializes a new instance of <see cref="CleanUpInsurance"/> built from the data specified. /// </summary> /// <exception cref="ArgumentException"> /// An <see cref="ArgumentException"/> is thrown if <paramref name="insuranceFile"/> and <paramref name="insuranceRegistryKey"/> don't match. /// </exception> /// <exception cref="ArgumentNullException"> /// An <see cref="ArgumentNullException"/> is thrown if both <paramref name="insuranceFile"/> and <paramref name="insuranceRegistryKey"/> are null. /// </exception> /// <param name="insuranceFile"></param> /// <param name="insuranceRegistryKey"></param> /// <param name="insuranceProcess"></param> private CleanUpInsurance(InsuranceFile insuranceFile, InsuranceRegistryKey insuranceRegistryKey, Process insuranceProcess) { _insuranceFile = insuranceFile; _insuranceProcess = insuranceProcess; _insuranceRegistryKey = insuranceRegistryKey; var flags = (_insuranceFile != null ? CleanUpInsuranceFlags.TrackByFile : CleanUpInsuranceFlags.None) | (_insuranceProcess != null ? CleanUpInsuranceFlags.ByWatchService : CleanUpInsuranceFlags.None) | (_insuranceRegistryKey != null ? CleanUpInsuranceFlags.TrackByRegistry : CleanUpInsuranceFlags.None); InsuranceBase insuranceBase = null; if (_insuranceFile != null) { insuranceBase = _insuranceFile; if (_insuranceRegistryKey != null) { if (!insuranceBase.MatchesWith(_insuranceRegistryKey, false)) throw new ArgumentException("The InsuranceFile and InsuranceRegistryKey don't match.", "insuranceFile"); insuranceBase.JoinWith(_insuranceRegistryKey); } } else if (_insuranceRegistryKey != null) insuranceBase = _insuranceRegistryKey; if (insuranceBase == null) throw new ArgumentNullException("insuranceFile", "At least one of both insuranceFile and insuranceRegistryKey needs to be initialized."); _assemblies = new List<AssemblyName>(insuranceBase.Assemblies); _timeStamp = insuranceBase.TimeStamp; _uniqueId = insuranceBase.InsuranceIdentifier; _data = new InsuranceData(insuranceBase.InstallerDescription, flags, _insuranceFile != null ? Path.GetDirectoryName(_insuranceFile.FileName) : null, _insuranceRegistryKey != null ? Path.GetDirectoryName(_insuranceRegistryKey.RegistryKeyName) : null, null); }