Exemple #1
0
 /// <summary>
 /// Remove association to the SoundArchive file extension
 /// </summary>
 public static void UnAssocFiles()
 {
     try
     {
         ShellFileType fileType = ShellFileType.GetType(FileExtension);
         fileType.DefaultIcon   = null;
         fileType.DefaultAction = null;
         fileType.Description   = null;
         fileType.MenuItems.Clear();
         fileType.Save();
     }
     catch (KeyNotFoundException)
     {
         // Missing file type, nothing to remove
     }
 }
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            try
            {
                if (args.Length > 0 && !args[0].StartsWith("--"))
                {
                    if (File.Exists(args[0]))
                    {
                        try
                        {
                            var cfgFile = INIFile.ParseFile(args[0]);
                            if (cfgFile != null && cfgFile.ContainsKey("shellextension"))
                            {
                                var      settings = cfgFile["shellextension"];
                                string[] required = new[] { "ext", "name", "displayname", "command" };
                                if (required.All(setting => settings.ContainsKey(setting)))
                                {
                                    string[] extensions   = settings["ext"].Split(',');
                                    string   actionName   = settings["name"];
                                    string   displayName  = settings["displayname"];
                                    string   command      = settings["command"];
                                    string[] dependencies = settings.ContainsKey("requires") ?
                                                            settings["requires"].Split(new string[] { "," },
                                                                                       StringSplitOptions.RemoveEmptyEntries) : new string[0];
                                    bool defaultAction = settings.ContainsKey("default") ?
                                                         settings["default"].ToLower() == "true" : false;

                                    if (ShellFileType.ActionExistsAny(extensions, actionName))
                                    {
                                        if ((args.Contains("--yes") || MessageBox.Show("Action '" + displayName + "' already exists.\nUninstall?",
                                                                                       AppDesc, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
                                        {
                                            if (dependencies.Length > 0)
                                            {
                                                HandleDependencies(dependencies, args[0], actionName, displayName, ref command, false);
                                            }
                                            ShellFileType.RemoveAction(extensions, actionName);
                                            MessageBox.Show("Action '" + displayName + "' has been successfully removed.",
                                                            AppDesc, MessageBoxButtons.OK, MessageBoxIcon.Information);
                                        }
                                    }

                                    else if (args.Contains("--yes") || MessageBox.Show(
                                                 "Install action '" + displayName + "' to the following file extensions?\n"
                                                 + String.Join(", ", extensions), AppDesc, MessageBoxButtons.YesNo,
                                                 MessageBoxIcon.Question) == DialogResult.Yes)
                                    {
                                        if (dependencies.Length > 0 &&
                                            !HandleDependencies(dependencies, args[0], actionName, displayName, ref command, true))
                                        {
                                            return;
                                        }
                                        ShellFileType.AddAction(extensions, actionName, displayName, command, defaultAction);
                                        MessageBox.Show("Action '" + displayName + "' has been successfully installed.",
                                                        AppDesc, MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("File '" + Path.GetFileName(args[0]) + "' has missing required fields: "
                                                    + String.Join(", ", required.Where(setting => !settings.ContainsKey(setting)).ToArray()),
                                                    AppDesc, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }
                            }
                            else
                            {
                                MessageBox.Show("File '" + Path.GetFileName(args[0]) + "' is not a valid shell extension file.",
                                                AppDesc, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                        catch (IOException)
                        {
                            MessageBox.Show("Cannot read '" + Path.GetFileName(args[0]) + "'.",
                                            AppDesc, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Cannot find '" + Path.GetFileName(args[0]) + "'.",
                                        AppDesc, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    if (!args.Contains("--install") &&
                        (args.Contains("--uninstall") || ShellFileType.ActionExists("seinf", "open")))
                    {
                        if (args.Contains("--yes") || MessageBox.Show(AppName
                                                                      + " is currently associated with .seinf files.\nRemove association?",
                                                                      AppDesc, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            args = new string[] { "--uninstall" };
                            using (ShellFileType seinf = ShellFileType.GetType("seinf"))
                            {
                                seinf.ProgId = null;
                            }
                            MessageBox.Show("File association has been removed.", AppDesc,
                                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    else
                    {
                        if (args.Contains("--yes") || MessageBox.Show(AppName
                                                                      + " is currently not associated with .seinf files. Associate?",
                                                                      AppDesc, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            args = new string[] { "--install" };
                            using (ShellFileType seinf = ShellFileType.GetOrCreateType("seinf"))
                            {
                                seinf.ProgId        = AppName + ".File";
                                seinf.Description   = "Shell Extension Information File";
                                seinf.DefaultIcon   = Assembly.GetEntryAssembly().Location + ",1";
                                seinf.DefaultAction = "open";
                                seinf.MenuItems.Clear();
                                seinf.MenuItems.Add("open",
                                                    new ShellFileType.MenuItem("&Install", "\"" + Assembly.GetEntryAssembly().Location + "\" \"%1\""));
                                seinf.MenuItems.Add("edit",
                                                    new ShellFileType.MenuItem("&Edit", "notepad \"%1\""));
                            }
                            MessageBox.Show("File association has been installed.", AppDesc,
                                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Something went wrong!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }