Skip to content
AlgorithmIntensity edited this page Jan 1, 2026 · 1 revision

🧩 Uninstall Tool Modules Documentation

📁 Modules/ Directory Structure

UninstallTool.Modules/
├── Uninstaller.cs       # Core uninstallation logic
├── ProcessManager.cs    # Process control and management
├── FileCleaner.cs       # File system cleanup operations
├── RegistryCleaner.cs   # Windows Registry operations
└── ServiceRemover.cs    # Windows Service management

🔧 1. Uninstaller.cs

Purpose

Complete application uninstallation with registry cleanup and file removal.

Source Code

namespace UninstallTool.Modules
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using Microsoft.Win32;

    public class Uninstaller
    {
        private const string AppName = "MyApplication";
        private const string RegistryKey =
            @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" + AppName;

        public void Execute()
        {
            StopProcesses();
            RemoveShortcuts();
            RemoveRegistryEntries();
            DeleteInstallationDirectory();
            RemoveFromProgramsList();
        }

        private void StopProcesses()
        {
            var processes = Process.GetProcessesByName(AppName);
            foreach (var process in processes)
            {
                process.Kill();
                process.WaitForExit(5000);
            }
        }

        private void RemoveShortcuts()
        {
            var startMenuPath = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.StartMenu),
                "Programs",
                AppName);

            var desktopPath = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                $"{AppName}.lnk");

            if (Directory.Exists(startMenuPath))
                Directory.Delete(startMenuPath, true);

            if (File.Exists(desktopPath))
                File.Delete(desktopPath);
        }

        private void RemoveRegistryEntries()
        {
            using (var key = Registry.LocalMachine.OpenSubKey(RegistryKey, true))
            {
                key?.DeleteValue("DisplayName", false);
                key?.DeleteValue("UninstallString", false);
                key?.DeleteValue("InstallLocation", false);
            }
            Registry.LocalMachine.DeleteSubKeyTree(RegistryKey, false);
        }

        private void DeleteInstallationDirectory()
        {
            var installDir = GetInstallDirectory();
            if (!string.IsNullOrEmpty(installDir) && Directory.Exists(installDir))
            {
                Directory.Delete(installDir, true);
            }
        }

        private void RemoveFromProgramsList()
        {
            var uninstallKey = Registry.LocalMachine.OpenSubKey(
                @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
                true);
            uninstallKey?.DeleteSubKeyTree(AppName, false);
        }

        private string GetInstallDirectory()
        {
            using (var key = Registry.LocalMachine.OpenSubKey(RegistryKey))
            {
                return key?.GetValue("InstallLocation") as string;
            }
        }
    }
}

Usage Example

var uninstaller = new Uninstaller();
uninstaller.Execute();

⚡ 2. ProcessManager.cs

Purpose

Windows process management and termination.

namespace UninstallTool.Modules
{
    using System.Diagnostics;

    public class ProcessManager
    {
        public void TerminateProcess(string processName)
        {
            foreach (var process in Process.GetProcessesByName(processName))
            {
                try
                {
                    if (!process.HasExited)
                    {
                        process.Kill();
                        process.WaitForExit(3000);
                    }
                }
                catch { }
            }
        }

        public void TerminateProcess(int processId)
        {
            try
            {
                var process = Process.GetProcessById(processId);
                if (!process.HasExited)
                {
                    process.Kill();
                    process.WaitForExit(3000);
                }
            }
            catch { }
        }

        public bool IsProcessRunning(string processName)
        {
            return Process.GetProcessesByName(processName).Length > 0;
        }

        public void WaitForProcessExit(string processName, int timeoutMs)
        {
            var processes = Process.GetProcessesByName(processName);
            foreach (var process in processes)
            {
                process.WaitForExit(timeoutMs);
            }
        }
    }
}

📁 3. FileCleaner.cs

Purpose

File system cleanup operations.

namespace UninstallTool.Modules
{
    using System.IO;

    public class FileCleaner
    {
        private const string AppName = "MyApplication";

        public void CleanAll()
        {
            CleanTempFiles();
            CleanAppData();
            CleanLocalAppData();
            CleanCommonAppData();
        }

        public void CleanTempFiles()
        {
            var appTempDir = Path.Combine(Path.GetTempPath(), AppName);
            DeleteDirectoryIfExists(appTempDir);
        }

        public void CleanAppData()
        {
            var appDataDir = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                AppName);
            DeleteDirectoryIfExists(appDataDir);
        }

        public void CleanLocalAppData()
        {
            var localAppDataDir = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                AppName);
            DeleteDirectoryIfExists(localAppDataDir);
        }

        public void CleanCommonAppData()
        {
            var commonAppDataDir = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
                AppName);
            DeleteDirectoryIfExists(commonAppDataDir);
        }

        private void DeleteDirectoryIfExists(string directoryPath)
        {
            if (Directory.Exists(directoryPath))
            {
                Directory.Delete(directoryPath, true);
            }
        }
    }
}

🔧 4. RegistryCleaner.cs

namespace UninstallTool.Modules
{
    using Microsoft.Win32;

    public class RegistryCleaner
    {
        private const string AppName = "MyApplication";

        public void CleanAll()
        {
            CleanUserRegistry();
            CleanMachineRegistry();
            CleanClassesRoot();
        }

        public void CleanUserRegistry()
        {
            CleanRegistry(Registry.CurrentUser, @"Software\" + AppName);
            RemoveRunEntry(
                Registry.CurrentUser,
                @"Software\Microsoft\Windows\CurrentVersion\Run");
        }

        public void CleanMachineRegistry()
        {
            CleanRegistry(Registry.LocalMachine, @"SOFTWARE\" + AppName);
            RemoveRunEntry(
                Registry.LocalMachine,
                @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
        }

        public void CleanClassesRoot()
        {
            CleanRegistry(Registry.ClassesRoot, AppName);
        }

        private void CleanRegistry(RegistryKey rootKey, string subKeyPath)
        {
            try
            {
                rootKey.DeleteSubKeyTree(subKeyPath, false);
            }
            catch { }
        }

        private void RemoveRunEntry(RegistryKey rootKey, string runKeyPath)
        {
            using (var runKey = rootKey.OpenSubKey(runKeyPath, true))
            {
                if (runKey != null && runKey.GetValue(AppName) != null)
                {
                    runKey.DeleteValue(AppName, false);
                }
            }
        }
    }
}

⚙️ 5. ServiceRemover.cs

namespace UninstallTool.Modules
{
    using System.Diagnostics;

    public class ServiceRemover
    {
        public void RemoveService(string serviceName)
        {
            try
            {
                StopService(serviceName);
                DeleteService(serviceName);
            }
            catch { }
        }

        private void StopService(string serviceName)
        {
            Process.Start("sc", $"stop \"{serviceName}\"")?.WaitForExit(10000);
        }

        private void DeleteService(string serviceName)
        {
            Process.Start("sc", $"delete \"{serviceName}\"")?.WaitForExit(10000);
        }
    }
}

🔗 Module Dependencies

graph TD
    A[Uninstaller] --> B[ProcessManager]
    A --> C[FileCleaner]
    A --> D[RegistryCleaner]
    A --> E[ServiceRemover]
Loading

⚠️ Important Notes

  • Administrator privileges required
  • Registry and file deletions are irreversible
  • Process termination is forceful