public void Delete()
        {
            string tmpFolder = Path.GetTempFileName();

            File.Delete(tmpFolder);
            Directory.CreateDirectory(tmpFolder);
            try
            {
                string xmlFileName = Path.Combine(tmpFolder, $"{scheme}.xml");
                using (var tempXMLFile = File.CreateText(xmlFileName))
                {
                    GenerateXMLFile(tempXMLFile);
                }
                switch (registerType)
                {
                case RegisterType.CurrentUser:
                {
                    var uninstallCommand         = new Command("xdg-mime", $"uninstall {xmlFileName} --novendor").Start().ThrowOnError();
                    var deleteDesktopFileCommand = new Command("rm", $"-f {Path.Combine(UserDir, $"{scheme}.desktop")}").Start().ThrowOnError();
                }
                break;

                case RegisterType.LocalMachine:
                {
                    var uninstallCommand         = new SudoCommand("xdg-mime", $"uninstall {xmlFileName} --mode system --novendor").Start().ThrowOnError();
                    var deleteDesktopFileCommand = new SudoCommand("rm", $"-f {Path.Combine(DefaultDir, $"{scheme}.desktop")}").Start().ThrowOnError();
                    var mimeapps = new MimeAppsList(RegisterType.LocalMachine);
                    var list     = mimeapps.GetURISchemeHandlers(scheme);
                    list.RemoveAll((s) => s == $"{scheme}.desktop");
                    mimeapps.Save();
                }
                break;
                }
            }
            finally
            {
                Directory.Delete(tmpFolder, true);
            }
        }
示例#2
0
 public ShopItem(string name, CurrencyAmount[] prices, SudoCommand command)
 {
     this.name    = name;
     this.prices  = prices;
     this.command = command;
 }
        public void Set()
        {
            string tmpFolder = Path.GetTempFileName();

            File.Delete(tmpFolder);
            Directory.CreateDirectory(tmpFolder);
            try
            {
                string xmlFileName     = Path.Combine(tmpFolder, $"{scheme}.xml");
                string desktopFileName = Path.Combine(tmpFolder, $"{scheme}.desktop");
                using (var tempXMLFile = File.CreateText(xmlFileName))
                {
                    GenerateXMLFile(tempXMLFile);
                }

                switch (registerType)
                {
                case RegisterType.CurrentUser:
                {
                    var installCommand = new Command("xdg-mime", $"install {xmlFileName} --novendor").Start().ThrowOnError();
                }
                break;

                case RegisterType.LocalMachine:
                {
                    var installCommand = new SudoCommand("xdg-mime", $"install {xmlFileName} --mode system --novendor").Start().ThrowOnError();
                }
                break;
                }

                using (var tempDesktopFile = File.CreateText(desktopFileName))
                {
                    //				[Desktop Entry]
                    //				Name=LMAO
                    //				Exec=/home/trung/lmao %u
                    //				Type=Application
                    //				NoDisplay=true
                    //				Categories=Utility;
                    //				MimeType=x-scheme-handler/lmao;
                    tempDesktopFile.WriteLine("[Desktop Entry]");
                    tempDesktopFile.WriteLine($"Name={name}");
                    tempDesktopFile.WriteLine($"Exec={exec}");
                    tempDesktopFile.WriteLine($"Type=Application");
                    tempDesktopFile.WriteLine("NoDisplay=true");
                    tempDesktopFile.WriteLine("Categories=Utility");
                    tempDesktopFile.WriteLine($"MimeType=x-scheme-handler/{scheme}");
                }

                switch (registerType)
                {
                case RegisterType.CurrentUser:
                {
                    var desktopFileCommand = new Command("desktop-file-install", $"{desktopFileName} --dir={UserDir}").Start().ThrowOnError();
                    var setDefaultCommand  = new Command("xdg-settings", $"set default-url-scheme-handler {scheme} {scheme}.desktop")
                                             .MapReturnValue(2, "runPath does not exist.")
                                             .Start()
                                             .ThrowOnError();
                }
                break;

                case RegisterType.LocalMachine:
                {
                    Console.WriteLine("Pause.");
                    Console.ReadLine();
                    var desktopFileCommand = new SudoCommand("desktop-file-install", $"{desktopFileName}").Start().ThrowOnError();
                    var mimeapps           = new MimeAppsList(RegisterType.LocalMachine);
                    var list = mimeapps.GetURISchemeHandlers(scheme);
                    list.Insert(0, $"{scheme}.desktop");
                    mimeapps.Save();
                }
                break;
                }
            }
            finally
            {
                Directory.Delete(tmpFolder, true);
            }
        }