Stall.Program.RegMain C# (CSharp) Method

RegMain() static private method

static private RegMain ( Options opts ) : int
opts Options
return int
        static int RegMain(Options opts)
        {
            string root = Path.Combine(Cache.LocalAppData, opts.AppName);
            var directory = new DirectoryInfo(root);

            // Create an uninstall.cmd script
            string keyName = $@"{Constants.UninstallKey}\{opts.AppName}";
            
            // Generate the commands to remove the shortcuts
            var shortcuts = GetShortcutFolders(opts).Select(f => Path.Combine(f, $"{opts.AppName}.url"));
            var deletions = shortcuts.Select(s => $@"del ""{s}""");
            string joined = string.Join(Environment.NewLine, deletions);

            string script = $@"@echo off
setlocal
cd %~dp0\..
:: Delete the shortcuts
{joined}
:: Use start to finish before we're deleted
start /min """" cmd /c ^
%= Use reg to delete the app key =% ^
reg delete ""HKCU\{keyName}"" /f ^& ^
%= Remove the app's root folder =% ^
rd /s /q ""{opts.AppName}""";
            string scriptPath = Path.Combine(root, "uninstall.cmd");
            File.WriteAllText(scriptPath, script);

            // Create the uninstaller Registry key
            if (!opts.Overwrite)
            {
                using (var key = Registry.CurrentUser.OpenSubKey(keyName))
                {
                    if (key != null) // OpenSubKey returns null if the key doesn't exist
                        return RaiseError(Errors.ExistingKey, $"Registry key {key} already exists! Exiting...");
                }
            }
            using (var key = Registry.CurrentUser.CreateSubKey(keyName))
            {
                // strings
                string path = opts.IconPath;
                if (path != null)
                {
                    // TODO: Support using the executable as an icon here, if it has one.
                    var status = Normalize.SpecialPath(root, ref path);
                    if (status != null)
                        return RaiseError(status);
                    key.SetValue("DisplayIcon", path);
                }

                key.SetValue("DisplayName", opts.AppName);
                if (opts.Version != null)
                    key.SetValue("DisplayVersion", opts.Version);
                key.SetValue("InstallDate", DateTime.Now.ToString("yyyymmdd"));
                key.SetValue("InstallLocation", root);
                if (opts.Publisher != null)
                    key.SetValue("Publisher", opts.Publisher);
                key.SetValue("QuietUninstallString", scriptPath);
                key.SetValue("UninstallString", scriptPath); // TODO: Show GIF of some sort?
                if (opts.ProjectUri != null)
                    key.SetValue("URLInfoAbout", opts.ProjectUri);
                if (opts.ReleasesUri != null)
                    key.SetValue("URLUpdateInfo", opts.ReleasesUri);

                // dwords
                key.SetValue("EstimatedSize", new DirectoryInfo(root).Size());
                key.SetValue("NoModify", 1);
                key.SetValue("NoRepair", 1);
                key.SetValue("Language", Constants.EnglishLocale);
            }

            return 0;
        }