9

My application has a picture box in it. When I open an image in Windows, instead of the default program that opens to show that image, I want to use my own application and have that program containing the picture box show the image.

Dominic K
  • 6,975
  • 11
  • 53
  • 62
Pedrum
  • 634
  • 3
  • 7
  • 16

4 Answers4

12

I did this recently, though I used a different action, not the default Open action.

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

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

Then you find out the path to your executable and build the "command string":

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

And register your executable to open files of that type:

String keyName = imgType + @"\shell\Open\command";
using (var key = Registry.ClassesRoot.CreateSubKey(keyName)) {
    key.SetValue("", command);
}
Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
5

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

So for example, if I wanted to associate .foo extensions with my Blah.exe program I would create the following registry entries (Note: In this case I'm associating .foo with a class called Foo.Document, then associated that class 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″
Dylan Smith
  • 22,069
  • 2
  • 47
  • 62
3

Your scenario makes me think that it's perfectly possible for you to simply select any JPG file, right click on the file, select "Open with" --> Choose default program, browse to your C# program and select the option "Always use the selected program to open this kind of file"

If you feel that you need to do this programmatically, you would need to set this on the Registry.

Here's a SO link that shows the process.

Community
  • 1
  • 1
Icarus
  • 63,293
  • 14
  • 100
  • 115
  • Just one WARNING: You may loose significant functionality, like printing, by doing this... so take note of the existing viewer settings so that you can restore them later, if needs be. – corlettk Oct 17 '11 at 04:26
  • The main and most important thing i wanna know is how the get the location or the path of that file i opened so i can get it in the picturebox but noone seems to be answering the QUESTION. – Pedrum Oct 17 '11 at 05:34
  • 4
    @user982850 no body answers the question because that's not what you asked. – Icarus Oct 17 '11 at 05:40
2

To set your app as default one for Jpeg files (example), you could:

  • Using Regedit.exe and goto item HKCR\.jpg\ and create subfolders like this shell\open\command.
  • Create there a new string value and edit default value as "path_to_exe" "%1"
  • Edit your EXE so that it can read arguments passed on command line and, if there is one, open it in picturebox (yeah, and many other controls as file does exists, file can be loaded in picturebox, etc...)
Marco
  • 56,740
  • 14
  • 129
  • 152
  • OK but the most important part is the part where I get the location(Path) of that file so I can get it in the Picturebox. how do I get the Path for the file that was opened ? – Pedrum Oct 17 '11 at 05:30
  • @user982850: in your software you have to get the first argument passed to you and use `Path.GetDirectory(arg)` – Marco Oct 17 '11 at 06:09