To register for my program (the program to read and process data from excel file), I did the following:

//Register ContextMenu
        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, the path parameter to the program:

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

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

with instance method:

public FormDemo()
        {
            InitializeComponent();
        }

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

In my project, Tool.cs file used to process data with excel file. If I do this then my program works well

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

            FormDemo main = new FormDemo();
            MainTool.frmmain = main;
            Application.Run(main);
}

And so my program has been registered on the ContextMenu, but when selecting the program, the program still can not process excel file.

Does anyone can guide me how to still use MainTool.frmmain (Process Excel method) and take the path as:

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

I'm having problems in the method void Main().

THANKS

I'm confused.

Are you:
1) Trying to get data from an Excel file
or
2) Trying to add the path to Excel to a context menu
or
3) Something different
?

Dear thines,

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.