public static AppPart GetInstalledEntryInfo(string partID) { using (var key = Registry.LocalMachine.OpenSubKey(WINDOWS_APPS_SUBKEY)) if (key.GetSubKeyNames().Count(c => c.Equals(partID)) > 0) { using (var currentKey = key.OpenSubKey(partID)) return(AppPart.ImportFromColumns(currentKey.GetValueNames().Select(c => new Tuple <string, object>(c, currentKey.GetValue(c))))); } return(null); }
public static void CreateLinks(AppPart part, params string[] linkPaths) { foreach (var current in linkPaths) { IShellLink link = (IShellLink) new ShellLink(); link.SetDescription(part.DisplayName); link.SetPath(part.ExecutablePath); link.SetWorkingDirectory(part.InstallLocation); ((IPersistFile)link).Save(current, false); } SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero); }
public static AppPart ImportFromColumns(IEnumerable <Tuple <string, object> > columns) { var result = new AppPart(); foreach (var current in typeof(AppPart).GetProperties()) { if (current.SetMethod != null) { var value = columns.FirstOrDefault(c => c.Item1 == current.Name).Item2; current.SetValue(result, current.PropertyType == typeof(bool) ? (int)value == 1 : value); } } return(result); }
public static void CreateApplicationEntry(AppPart part) { using (RegistryKey key = Registry.LocalMachine.OpenSubKey(WINDOWS_APPS_SUBKEY, true)) if (key.GetSubKeyNames().Count(c => c.Equals(part.ID)) == 0) { using (var CreatedKey = key.CreateSubKey(part.ID)) foreach (var current in part.Columns) { CreatedKey.SetValue(current.Item1, current.Item2); } } else { RemoveApplicationEntry(part.ID); CreateApplicationEntry(part); } }
async Task UninstallApp() { PreInstallTB.Visibility = Visibility.Visible; PreInstallTB.Text = "Подготовка к удалению...\nЗакройте все окна приложения"; await Task.Run(() => { var myID = Process.GetCurrentProcess().Id; while (Process.GetProcesses().Any(c => c.ProcessName.ToLower().Contains("incollege") && c.Id != myID)) { Thread.Sleep(100); } }); foreach (var currentComponent in InstallComponents.Select(c => RegistryManager.GetInstalledEntryInfo(c.ID))) { if (currentComponent != null) { var files = Directory.GetFiles(CurrentDirectory).Where(c => c.EndsWith("APM")).ToArray(); if (files.Length > 0) { AppPart localComponent = null; await Task.Run(() => { while (true) { try { using (var stream = new FileStream(files[0], FileMode.Open)) { localComponent = ((AppPart) new BinaryFormatter().Deserialize(stream)); stream.Close(); } break; } catch (IOException) { Thread.Sleep(100); } } }); if (currentComponent.ID == localComponent.ID) { PreInstallTB.Text = $"Удаление \"{currentComponent.DisplayName}\"..."; await Task.Run(() => { foreach (var current in Directory.GetFiles(currentComponent.InstallLocation)) { if (current != MyFileName) { try { File.Delete(current); } catch (IOException) { } } } RegistryManager.RemoveApplicationEntry(currentComponent.ID); LNKManager.RemoveLinks(Path.Combine(CommonVariables.DesktopPath, $"{currentComponent.DisplayName}.lnk")); Process.Start(new ProcessStartInfo("cmd", $" /C timeout 10&&rd /s /q \"{CurrentDirectory}\"") { CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden }); }); break; } } } } PreInstallTB.Text = "Удаление успешно завершено!"; await Task.Run(() => Thread.Sleep(2000)); Process.GetCurrentProcess().Kill(); }