How to make a C # application work by default for certain files?

My application has a window with pictures. When I open an image in Windows, instead of the default program that opens to display this image, I want to use my own application and show that the program containing the image window displays the image.

+4
source share
4 answers

I did this recently, although I used a different action than the default Open action.

First you find out the file type of some extension, say .jpg:

var imgKey = Registry.ClassesRoot.OpenSubKey(".jpg") var imgType = key.GetValue(""); 

Then you will find out the path to the executable file and build the "command line":

 String myExecutable = Assembly.GetEntryAssembly().Location; String command = "\"" + myExecutable + "\"" + " \"%1\""; 

And register your executable file to open files of this type:

 String keyName = imgType + @"\shell\Open\command"; using (var key = Registry.ClassesRoot.CreateSubKey(keyName)) { key.SetValue("", command); } 
+8
source

You need to make some entries in the registry. First you need to associate the file extension with the class name (the class name can be anything, just run it).

So, for example, if I wanted to associate the .foo extensions with my Blah.exe program, I would create the following registry entries (Note: In this case, I associate .foo with the Foo.Document class, then this class is associated with my program):

 Key: HKLM\SOFTWARE\Classes\.foo Value: <default> = "Foo.Document" Key: HKLM\SOFTWARE\Classes\Foo.Document Value: <default> = "Foo Document" Key: HKLM\SOFTWARE\Classes\Foo.Document\shell\open\command Value: <default> = "[blah.exe]" "%1″ 
+5
source

In your scenario, it seems to me that you can simply select any JPG file, right-click on the file, select "Open with" → Select the default program, go to your C # program and select the option "Always use the selected program for open this type of file "

If you think you need to do this programmatically, you need to install it in the registry.

Here is the SO> link showing the process.

+3
source

To configure the default application for Jpeg files (example), you can:

  • Using Regedit.exe and goto item HKCR\.jpg\ and creating subfolders like shell\open\command .
  • Create a new row value and change the default value as "path_to_exe" "%1"
  • Edit the EXE so that it can read the arguments passed on the command line and, if any, open it in the image window (yes, and many other controls, since the file exists, the file can be loaded in the image window, etc. ) )
+1
source

All Articles