private void CreateFileInsurance() { Directory.CreateDirectory(_data.TrackingFilesFolder); _insuranceFile = new InsuranceFile(Path.Combine(_data.TrackingFilesFolder, _uniqueId.ToString()), _uniqueId, _data.Installer, LocalMachine.Identifier, _timeStamp, _assemblies); InsuranceFile.Write(_insuranceFile); }
/// <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)); }
/// <summary> /// Writes the specified <see cref="InsuranceFile"/> to the file with the specified <see cref="FileName"/>. /// </summary> /// <param name="insuranceFile"></param> public static void Write(InsuranceFile insuranceFile) { using (var str = File.Open(insuranceFile.FileName, FileMode.Create, FileAccess.Write)) { using (var writer = new StreamWriter(str)) { writer.WriteLine("GUID=\"{0}\"" + Environment.NewLine + "MachineId=\"{1}\"" + Environment.NewLine + "CreationDateTime=\"{2}\"" + Environment.NewLine, insuranceFile.InsuranceIdentifier, insuranceFile.MachineId, insuranceFile.TimeStamp.ToString(_DateTimeFormat)); writer.WriteLine("Installer=[Type=\"{0}\", Id=\"{1}\", Description=\"{2}\"]" + Environment.NewLine, insuranceFile.InstallerDescription.Type, insuranceFile.InstallerDescription.Id, insuranceFile.InstallerDescription.Description); foreach (var item in insuranceFile.Assemblies) writer.WriteLine(item); writer.Flush(); } } }
/// <summary> /// Writes the specified <see cref="InsuranceFile"/> to the file with the specified <see cref="FileName"/>. /// </summary> /// <param name="insuranceFile"></param> public static void Write(InsuranceFile insuranceFile) { using (var str = File.Open(insuranceFile.FileName, FileMode.Create, FileAccess.Write)) { using (var writer = new StreamWriter(str)) { writer.WriteLine("GUID=\"{0}\"" + Environment.NewLine + "MachineId=\"{1}\"" + Environment.NewLine + "CreationDateTime=\"{2}\"" + Environment.NewLine, insuranceFile.InsuranceIdentifier, insuranceFile.MachineId, insuranceFile.TimeStamp.ToString(_DateTimeFormat)); writer.WriteLine("Installer=[Type=\"{0}\", Id=\"{1}\", Description=\"{2}\"]" + Environment.NewLine, insuranceFile.InstallerDescription.Type, insuranceFile.InstallerDescription.Id, insuranceFile.InstallerDescription.Description); foreach (var item in insuranceFile.Assemblies) { writer.WriteLine(item); } writer.Flush(); } } }
/// <summary> /// Returns a list of <see cref="InsuranceFile"/>s which are read from the specified <paramref name="folder"/>. /// </summary> /// <param name="folder"></param> /// <returns></returns> private static List <InsuranceFile> GetFileInsurances(string folder) { if (!Directory.Exists(folder)) { return(new List <InsuranceFile>(0)); } var identifiers = Directory.GetFiles(folder); var files = new List <InsuranceFile>(); foreach (var file in identifiers) { InsuranceFile insuranceFile; if (InsuranceFile.TryRead(file, out insuranceFile)) { files.Add(insuranceFile); } } return(files); }
/// <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> /// Tries to build an instance of <see cref="InsuranceFile"/> from data read from the specified file. /// </summary> /// <exception cref="FileNotFoundException"></exception> /// <param name="fileName"></param> /// <param name="insuranceFile"></param> /// <returns></returns> public static bool TryRead(string fileName, out InsuranceFile insuranceFile) { insuranceFile = null; using (var str = File.Open(fileName, FileMode.Open, FileAccess.Read)) { using (var reader = new StreamReader(str)) { string guidValue, machineId, creationDateTime; // Get GUID var line = reader.ReadLine(); if (!ReadValue(line, "GUID", "\"", out guidValue)) { return(false); } // Get MachineId line = reader.ReadLine(); if (!ReadValue(line, "MachineId", "\"", out machineId)) { return(false); } // Get CreationDate line = reader.ReadLine(); if (!ReadValue(line, "CreationDateTime", "\"", out creationDateTime)) { return(false); } // Skip empty line reader.ReadLine(); // Read the InstallerDescription from the file var installer = ReadInstallerDescriptionFromLine(reader.ReadLine()); if (installer == null) { return(false); } // Skip empty line reader.ReadLine(); // Read the list of insured assemblies var assemblies = new List <AssemblyName>(); while (!reader.EndOfStream) { line = reader.ReadLine(); if (line == "") { break; } try { assemblies.Add(new AssemblyName(line)); } catch (ArgumentException) { return(false); } } // Construct the GUID Guid guid; try { guid = new Guid(guidValue); } catch { return(false); } insuranceFile = new InsuranceFile(fileName, guid, installer, machineId, DateTime.Parse(creationDateTime), assemblies); return(true); } } }
/// <summary> /// Tries to build an instance of <see cref="InsuranceFile"/> from data read from the specified file. /// </summary> /// <exception cref="FileNotFoundException"></exception> /// <param name="fileName"></param> /// <param name="insuranceFile"></param> /// <returns></returns> public static bool TryRead(string fileName, out InsuranceFile insuranceFile) { insuranceFile = null; using (var str = File.Open(fileName, FileMode.Open, FileAccess.Read)) { using (var reader = new StreamReader(str)) { string guidValue, machineId, creationDateTime; // Get GUID var line = reader.ReadLine(); if (!ReadValue(line, "GUID", "\"", out guidValue)) return false; // Get MachineId line = reader.ReadLine(); if (!ReadValue(line, "MachineId", "\"", out machineId)) return false; // Get CreationDate line = reader.ReadLine(); if (!ReadValue(line, "CreationDateTime", "\"", out creationDateTime)) return false; // Skip empty line reader.ReadLine(); // Read the InstallerDescription from the file var installer = ReadInstallerDescriptionFromLine(reader.ReadLine()); if (installer == null) return false; // Skip empty line reader.ReadLine(); // Read the list of insured assemblies var assemblies = new List<AssemblyName>(); while (!reader.EndOfStream) { line = reader.ReadLine(); if (line == "") break; try { assemblies.Add(new AssemblyName(line)); } catch (ArgumentException) { return false; } } // Construct the GUID Guid guid; try { guid = new Guid(guidValue); } catch { return false; } insuranceFile = new InsuranceFile(fileName, guid, installer, machineId, DateTime.Parse(creationDateTime), assemblies); return true; } } }
private void CreateFileInsurance() { Directory.CreateDirectory(_data.TrackingFilesFolder); _insuranceFile = new InsuranceFile(Path.Combine(_data.TrackingFilesFolder, _uniqueId.ToString()), _uniqueId, _data.Installer, LocalMachine.Identifier, _timeStamp, _assemblies); InsuranceFile.Write(_insuranceFile); }
/// <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); }