Example #1
0
        public static void Main()
        {
            if (SingleInstance <App> .InitializeAsFirstInstance(Unique))
            {
                var application = new App();

                // Register Shell Context Menu

                // full path to self, %L is placeholder for selected file
                string menuCommand = string.Format(
                    "\"{0}\" \"%L\"", System.Reflection.Assembly.GetExecutingAssembly().Location);

                // register the context menu
                FileShellExtension.Register(App.FileType,
                                            App.KeyName, App.MenuText,
                                            menuCommand);

                // register the context menu
                FileShellExtension.Register("Directory",
                                            App.KeyName, App.MenuText,
                                            menuCommand);

                application.InitializeComponent();
                application.Run();

                // Allow single instance code to perform cleanup operations
                SingleInstance <App> .Cleanup();
            }
        }
Example #2
0
        private void registerContextHandlerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var menuCommand = $"\"{Application.ExecutablePath}\" \"%L\"";

            FileShellExtension.Register("dllfile", "OleWoo Context Menu",
                                        "Open with OleWoo", menuCommand);
        }
Example #3
0
        private void buttonRegister_Click(object sender, EventArgs e)
        {
            try
            {
                if (FileShellExtension.IsRegistered())
                {
                    FileShellExtension.UnRegister();
                }
                else
                {
                    FileShellExtension.Register();
                }
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("Please run Application as local Admin");
            }

            buttonRegister.Text = FileShellExtension.IsRegistered() ? "Unregister ShellExtensions" : "Register ShellExtensions";
        }
Example #4
0
 private void ForceUnregister_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         FileShellExtension.Unregister("*", RegistryKey);
     }
     catch (Exception ex)
     {
         MessageBox.Show("Failed to unregister File Context Menu: " + ex.Message, "Error", MessageBoxButton.OK,
                         MessageBoxImage.Error);
     }
     try
     {
         FileShellExtension.Unregister("Directory", RegistryKey);
     }
     catch (Exception ex)
     {
         MessageBox.Show("Failed to unregister Folder Context Menu: " + ex.Message, "Error", MessageBoxButton.OK,
                         MessageBoxImage.Error);
     }
 }
Example #5
0
 public OptionsControl()
 {
     InitializeComponent();
     buttonRegister.Text = FileShellExtension.IsRegistered() ? "Unregister ShellExtensions" : "Register ShellExtensions";
 }
Example #6
0
 private void unregisterContextHandlerToolStripMenuItem_Click(object sender, EventArgs e)
 {
     FileShellExtension.Unregister("dllfile", "OleWoo Context Menu");
 }
Example #7
0
    private static bool HandleInput(IList <string> args)
    {
        // extract this program's guid to use as the registry key for insertion/extraction
        var assembly      = Assembly.GetExecutingAssembly();
        var guidAttribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0];

        var handled = false;
        var message = "";

        // register
        if (args.Count == 0 || string.Compare(args[0], "-register", StringComparison.OrdinalIgnoreCase) == 0)
        {
            // keyword detected, do not process as image
            handled = true;

            // elevation is required for registry access
            if (!IsElevated())
            {
                message = "Registering requires administrator rights.  Run as administrator and try again.";
            }
            else
            {
                // full path to self, %L is placeholder for selected file
                var menuCommand = $"\"{Application.ExecutablePath}\" \"%L\"";

                // the icon gets copied to the same output directory, so find it there
                var iconPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), IconFilename);

                // register the context menu for each file type
                try
                {
                    foreach (var t in FileTypes)
                    {
                        FileShellExtension.Register(t, guidAttribute.Value, MenuText, menuCommand, iconPath);
                    }

                    message = $"Registration successful.  \"{MenuText}\" will appear as an option on the context menu.";
                }
                catch (ArgumentException)
                {
                    message = "Unable to modify registry.  No operation performed.";
                }
                catch (AccessViolationException)
                {
                    message = "Insufficient permission to modify registry.  Check privileges and policy.";
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                }
            }
        }
        // unregister
        else if (string.Compare(args[0], "-unregister", StringComparison.OrdinalIgnoreCase) == 0)
        {
            handled = true;

            // elevation is required for registry access
            if (IsElevated() == false)
            {
                message = "Unregistering requires administrator rights.";
            }
            else
            {
                try
                {
                    // unregister the context menu for each file type
                    foreach (var t in FileTypes)
                    {
                        FileShellExtension.Unregister(t, guidAttribute.Value);
                    }

                    message = "Successfully unregistered.  Context menu entry removed.";
                }
                catch (ArgumentException)
                {
                    message = "Unable to modify registry.  No operation performed.";
                }
                catch (AccessViolationException)
                {
                    message = "Insufficient permission to modify registry.  Check privileges and policy.";
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                }
            }
        }

        if (!string.IsNullOrEmpty(message))
        {
            MessageBox.Show(message);
        }

        // command line did not contain an action
        return(handled);
    }