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!

Dani AI

Generated

Short diagnosis and what’s missing

The context-menu entry is only the launcher; Explorer starts the app and passes the selected filename as a command-line argument. The thread shows already created the menu entry (it appears in the right-click menu), so the usual reasons the file’s contents don’t load are: the registry command does not pass the filename placeholder, the app ignores/does not parse command-line arguments, or the app is single‑instance and quits the second instance without forwarding the filename to the running instance. ’s pointer to the registry is correct; ’s pointer to an Excel-reading library is also useful for the file-processing side.

Checklist of concrete fixes

  • Verify the command string stored under the file type’s ProgID shell\YourVerb\command includes the file placeholder (use "%1" for a single filename or "%*" for all args) and that the executable path is quoted (paths with spaces will break otherwise).
  • Prefer writing per-user keys under HKCU\Software\Classes to avoid UAC prompts; per-machine keys go under HKCR/HKLM and need admin. Use the installer project’s Registry view so keys are created on install and removed on uninstall.
  • Confirm the ProgID you patch matches the extension (.xls vs .xlsx often map to different ProgIDs).

Make the app open the passed filename

Ensure the app reads the args passed by Explorer and hands each path to the existing Excel-processing routine. Example startup sketch:

static void Main(string[] args)
{
    // initialize app...
    if (args != null && args.Length > 0)
    {
        foreach (var path in args)
            ExcelLoader.LoadFromFile(path); // call existing import routine
    }
    Application.Run(new MainForm());
}

Notes on single‑instance apps and debugging

If the program enforces single-instance, implement a forwarding mechanism (named pipe, Windows message, or the VB single-instance application model) so the second process sends its args to the first instead of exiting. For quick debugging, run the EXE from a command prompt with a filename, or show/log Environment.GetCommandLineArgs() on startup to confirm what Explorer is passing.

Recommended Answers

All 9 Replies

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");
      }
   }
}

Try this:
Found it by using the rearch function(top, right) of this site. Many more related items fcan be found.

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.