static void Main(string[] args) { try { //ShellFileType.AddAction("txt", "testaction", "Testing FileActionsManager", "cmd.exe /C echo %1 && pause > nul"); //ShellFileType.RemoveAction("txt", "testaction"); if (args.Length == 3 && args[0] == "del") { ShellFileType.RemoveAction(args[1].Split(','), args[2]); } else if ((args.Length == 5 || args.Length == 6) && args[0] == "add") { ShellFileType.AddAction(args[1].Split(','), args[2], args[3], args[4], args.Length == 6 && args[5] == "default"); } else { string exeName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName); Console.WriteLine(" - Windows Shell Menu Action Setter v1.0 - By ORelio -\n"); Console.WriteLine("Usage: " + exeName + " add <ext> <int> <dsp> <cmd> [def]"); Console.WriteLine("Usage: " + exeName + " del <ext> <int>\n"); Console.WriteLine("add : Add or update the action based on internal name"); Console.WriteLine("del : Remove the action based on internal name"); Console.WriteLine("ext : List of file extensions to affect eg mp3 or mp3,mp4 and so on"); Console.WriteLine("int : internal name to designate the action"); Console.WriteLine("dsp : display name of the shell menu item"); Console.WriteLine("cmd : command to execute when selecting item. File is provided as %1"); Console.WriteLine("def : add `default' as last argument for setting the action as the default one"); } } catch (UnauthorizedAccessException) { RelaunchAsAdmin(args, true); } }
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); } }