Manh Thao

Manh Thao

  • NA
  • 11
  • 0

Adding program to context menu

Feb 29 2012 2:46 AM
To add my program into the context menu (when the user right click on excel file). I used the following code:

[CODE]public static bool AddContextMenuItem(string Extension, string MenuName, string MenuDescription, string MenuCommand)
        {
            bool ret = false;
            RegistryKey rkey = Registry.ClassesRoot.OpenSubKey(Extension);

            if (rkey != null)
            {
                string extstring = rkey.GetValue("").ToString();

                rkey.Close();

                if (extstring != null)
                {
                    if (extstring.Length > 0)
                    {
                        rkey = Registry.ClassesRoot.OpenSubKey(extstring, true);

                        if (rkey != null)
                        {
                            string strkey = "shell\\" + MenuName + "\\command";

                            RegistryKey subky = rkey.CreateSubKey(strkey);

                            if (subky != null)
                            {
                                subky.SetValue("", MenuCommand);
                                subky.Close();
                                subky = rkey.OpenSubKey("shell\\" + MenuName, true);
                                if (subky != null)
                                {
                                    subky.SetValue("", MenuDescription);
                                    subky.Close();
                                }
                                ret = true;
                            }
                            rkey.Close();
                        }
                    }
                }
            }

            return ret;
        }[/CODE]

and:

[CODE]public static bool ProcessCommand(string[] args)
        {
           
            if (args.Length == 0 || string.Compare(args[0], "-register", true) == 0)
            {
               
                string menuCommand = string.Format("\"{0}\" \"%L\"", Application.ExecutablePath);               

                //frmMain.Register(Program.FileType, Program.KeyName, Program.MenuText, menuCommand);               
               
                AddContextMenuItem(FileType, KeyName, MenuText, menuCommand);
               
                MessageBox.Show(string.Format("The {0} shell extension was registered.", Program.KeyName), Program.KeyName);

                return true;
            }         
           
            return false;
        }[/CODE]

I used the following function to retrieve the excel file path has been rightclick choose to load the program:

[CODE]public static void OpenExcelFileWithEasyForm(string filePath)
        {
            try
            {               
                string excelFilePath = Path.Combine(Path.GetDirectoryName(filePath), string.Format("{0} (EasyForm){1}", Path.GetFileNameWithoutExtension(filePath), Path.GetExtension(filePath)));
                            
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("An error occurred: {0}", ex.Message), Program.KeyName);
                return;
            }
        }[/CODE]

In main():

[CODE]if (!ProcessCommand(args))
            {
                OpenExcelFileWithEasyForm(args[0]);
            }[/CODE]

But I still do not load the excel file into my program by selecting the program from ContextMenu (right click on excel file and choose program).
Although if select the path to excel file from the program, the data from excel file is loaded into the program.

I'm still missing something at function OpenExcelFileWithEasyForm(). Has anyone ever done this, please guide me to complete this function to load the excel file into my program when selecting programs in ContextMenu.

Thanks