1

I'm using WinForms. I'm trying to make a simple image viewer. In my Form I have a PictureBox. I want to be able to right click on any picture in my computer, Click on "Open With," click on my application from the list and the picture would open in my application.

    [DllImport("Shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);

    public Form1()
    {
        InitializeComponent();
    }

    public static bool IsAssociated()
    {
        return (Registry.CurrentUser.OpenSubKey("Software\\Classes\\.tif", false) == null);
    }

    public static void Associate()
    {
        RegistryKey FileReg = Registry.CurrentUser.CreateSubKey("Software\\Classes\\.tif");
        RegistryKey AppReg = Registry.CurrentUser.CreateSubKey("Software\\Classes\\Application\\How_To_Open_File_With_Associated_Applicaiton.exe");
        RegistryKey AppAssoc = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.tif");

        FileReg.CreateSubKey("DefaultIcon").SetValue("", "C:\\Users\\Timmy\\Pictures\\Icons_Folder\\I-View-Icon_V2.ico");//Path to icon
        FileReg.CreateSubKey("PerceivedType").SetValue("", "image"); //Something that the file holds. 

        AppReg.CreateSubKey("shell\\open\\command").SetValue("", "\"" + Application.ExecutablePath + "\" %");
        AppReg.CreateSubKey("shell\\edit\\command").SetValue("", "\"" + Application.ExecutablePath + "\" %");
        AppReg.CreateSubKey("DefaultIcon").SetValue("", "C:\\Users\\Timmy\\Pictures\\Icons_Folder\\I-View-Icon_V2.ico");

        AppAssoc.CreateSubKey("UserChoice").SetValue("Progid", "Application\\How_To_Open_File_With_Associated_Applicaiton.exe");
        SHChangeNotify(0x08000000 , 0x0000, IntPtr.Zero, IntPtr.Zero); 

    }

Example: Below I'm opening the picture in paint for a demonstration.

Goal: Have the ability to open .tif images into my picturebox in my application.

References
These are some examples I've researched online to try to do the task I'm trying to do, but i find my self getting stuck on how to tell the program, "Now take this .tif image and open it up in the picturebox":

Associate File Extension with Application

https://www.youtube.com/watch?v=XtYobuVvcFE

http://www.codeproject.com/Articles/43675/C-FileAssociation-Class


enter image description here

Community
  • 1
  • 1
taji01
  • 2,527
  • 8
  • 36
  • 80

1 Answers1

0

You need to register your executable as a handler for that filetype/extension.

https://msdn.microsoft.com/en-us/library/windows/desktop/hh127451(v=vs.85).aspx

hometoast
  • 11,522
  • 5
  • 41
  • 58