Manh Thao

Manh Thao

  • NA
  • 11
  • 0

How to get the path parameter in voidMain function (Add program to Context Menu)

Mar 16 2012 10:17 PM
I will describe the step by step and my problems encountered when registering my program in ContextMenu.

In my solution, class MainTool used to process data in Excel File: read the content, logic, catch errors, exceptions, ... And when loading Excel file contents into the program must go through this class, so void main() function of I as follows:

static void Main(string[] args)
{

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

frmMain main = new frmMain();
MainTool.frmmain = main;

Application.Run(main);

}

And now, I am trying to register my program on ContextMenu (more comfortable for the user to choose a program to manipulate the right click directly on the Excel file instead of selecting from the program file Excel). I did the following:

public static void RemoveContextMenuItem(string extension, string menuName)
        {
            RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(extension);

            if (registryKey != null)
            {
                string extstring = registryKey.GetValue("").ToString(); //root

                registryKey.Close();

                if (!string.IsNullOrEmpty(extstring))
                {
                    registryKey = Registry.ClassesRoot.OpenSubKey(extstring, true);
                    if (registryKey != null)
                    {
                        RegistryKey subky = registryKey.OpenSubKey("shell\\" + menuName);
                        if (subky != null)
                        {
                            subky.Close();
                            registryKey.DeleteSubKeyTree("shell\\" + menuName);
                            registryKey.Close();
                        }
                    }
                }
            }
        }

and Instance method of frmMain:

public frmMain()
        {
            InitializeComponent();
        }

        public frmMain(string filePath)
        {
            InitializeComponent();
        }

Last job is to take the path of the Excel file and pass in the Main function:

[STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (args != null && args.Length > 0)
            {
                Application.Run(new frmMain(args[0]));
            }
            else Application.Run(new frmMain());
        }

Registration program has been in ContextMenu, when selected to run the program but can not read excel file as well as data processing on it. I understand that, class MainTool not yet been assigned to frmMain. And I do not know how to solve this problem.

Or better way to register the program on ContextMenu when packaged applications that can meet the above purposes.

Thanks you very much!

Answers (1)