How to open excel file with the program when the user selected program to open excel?

I have a software to process data in excel file (*.xls, *.xlsx). My software loaded excel file to process by selecting the path to excel file.

For convenience than I intend to add choices for user by user can right-click into excel file and choose my program to open (Open with ... "My software name").

So users can choose my program as default or not default to open excel file!

I don't know how to do it! Look forward to your guidance!

If you're talking about the open-with-list, it's in the System Registry.
\HKEY_LOCAL_MACHINE\Software\Classes\*\OpenWithList
If you're familiar with Registry Editing, you can make the addition of the key there.

Using codes C# in my program, I wanna add a shortcut of my c# program when i right click of a specific files like xls, xlsx to open the excel file with my program!!!

I'm writing software to handle data from excel file.

The problem I encountered when packing to install the program how to add the icon' program in the context menu (user must use right click into excel file to select the icon of the program) and program will read excel file (similar to the operation of unzip files - winrar) and also when unistall the program's icon in context menu will be removed.


THANKS!!!

Are you saying the only need of the program to be opened in Excel is to choose an icon? If so, why not launch Excel and open the file from your code?


Otherwise -- (on the registry issue (if it is still valid)):
If you don't mind the user "accepting" the registry entries, you could put all of the necessary values in a .reg file and start that as a process from inside your code.
The user will receive a prompt, but it is an easy method.
Example (let's say you have a .TTXT file you want opened with Excel).
You might need a few registry entries to make that happen.
TTXT.REG file:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.ttxt]
@="ttxt_auto_file"

[HKEY_CLASSES_ROOT\ttxt_auto_file]
@=""

[HKEY_CLASSES_ROOT\ttxt_auto_file\shell]

[HKEY_CLASSES_ROOT\ttxt_auto_file\shell\open]

[HKEY_CLASSES_ROOT\ttxt_auto_file\shell\open\command]
@="\"C:\\Program Files\\Microsoft Office\\Office12\\EXCEL.EXE\" \"%1\""

[HKEY_CURRENT_USER\Software\Classes\.ttxt]
@="ttxt_auto_file"

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ttxt]

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ttxt\OpenWithList]
"a"="EXCEL.EXE"
"MRUList"="a"

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ttxt\OpenWithProgids]
"ttxt_auto_file"=hex(0):

[HKEY_CURRENT_USER\Software\Classes\ttxt_auto_file]
@=""

[HKEY_CURRENT_USER\Software\Classes\ttxt_auto_file\shell]

[HKEY_CURRENT_USER\Software\Classes\ttxt_auto_file\shell\open]

[HKEY_CURRENT_USER\Software\Classes\ttxt_auto_file\shell\open\command]
@="\"C:\\Program Files\\Microsoft Office\\Office12\\EXCEL.EXE\" \"%1\""

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ttxt]

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ttxt\OpenWithList]
"a"="EXCEL.EXE"
"MRUList"="a"

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ttxt\OpenWithProgids]
"ttxt_auto_file"=hex(0):

You could use a small amount of code to launch that reg file:

using System.Diagnostics;
namespace DW_413250_CS_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         Process.Start("c:\\science\\ttxt.reg");
      }
   }
}

Dear thines,
I can open Excel file from my program (from File - Open and select the path to excel file)- This is method 1
However, for convenience and comfort for users, I will develop a feature from excel file I can right-click it and select the icon my program, since then all data from excel file will be loaded into my program, this is exactly the same as the first way is to select the excel file from the program - This is method 2 (2 ways are handled the same, just different ways to manipulate).

With your instructions above. I did not quite understand how to manipulate with registry. I will package to install the program to the user (using visual setup project). When the installation is for users of any computer must also meet the above purposes (2 ways to manipulate the excel file)

Thanks!!!

And just for a later time, try to avoid using the word 'Help!' alone as a topic for your thread...

commented: You read the rules! +15

I tried the following code:

public 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 call it:

AddContextMenuItem(".xls", "EasyForm", "Open with &EasyForm", menuCommand);

However, I still can not load content from the excel file.

And the program still does not load the contents of the excel file when selecting the program from the context menu (choose program in context menu when right click on excel file).

http://i214.photobucket.com/albums/cc199/linhvi/1-1.jpg

http://i214.photobucket.com/albums/cc199/linhvi/2-2.jpg

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.