Thread: Adding my program to contextmenu

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    1

    Adding my program to contextmenu

    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;
            }
    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;
            }
    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;
                }
            }
    In main():

    Code:
    if (!ProcessCommand(args))
                {
                    OpenExcelFileWithEasyForm(args[0]);
                }
    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

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    So in OpenExcelFileWithEasyForm() you've created the path to your EasyForm, but you're not doing anything with it. If you want to open the file using the default program that's registered with Windows for that file extension, you can just do this:
    Code:
    System.Diagnostics.Process.Start(excelFilePath);
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ContextMenu SubMenu not displaying on Hover
    By motocross1 in forum C++ Programming
    Replies: 13
    Last Post: 04-10-2011, 11:48 AM
  2. Adding a Map to a program
    By Shogun32 in forum C++ Programming
    Replies: 1
    Last Post: 05-04-2009, 09:42 AM
  3. Help Please adding a counter to my Program
    By John_Fev in forum C++ Programming
    Replies: 1
    Last Post: 04-02-2009, 09:29 PM
  4. duplicating the desktop ContextMenu
    By Mastadex in forum Windows Programming
    Replies: 1
    Last Post: 07-07-2007, 02:10 AM
  5. Text box default contextmenu
    By ChadJohnson in forum Windows Programming
    Replies: 6
    Last Post: 03-28-2005, 02:44 PM

Tags for this Thread