Click here to Skip to main content
11,806,575 members (40,302 online)
Click here to Skip to main content

Calling the Open With dialog box from your application, using C#

, 17 Feb 2006 CPOL 101.2K 1.4K 41
Rate this:
Please Sign up or sign in to vote.
This article illustrates calling the Open With dialog box from your application, using C#.


This article is to introduce shell programming with C# for beginners in .NET. The Shell namespace organizes the file system and other objects managed by the Shell into a single tree-structured hierarchy. Conceptually, the Shell namespace is essentially a larger and more inclusive version of the file system. The Shell namespace objects include file system folders and files, along with "virtual" objects, such as the Recycle Bin and Printers folders. One of the primary responsibilities of the Shell is managing and providing access to the wide variety of objects that make up the system. Here, I have created a sample for demonstrating how to call the Open with dialog box in your Windows system.

Overview of the solution

Here in this article, I have developed a simple image viewer application to demonstrate the Open with dialog box in Windows, using C#. It’s really hard to explain the Shell concepts. This application is developed using Microsoft Visual C# Express Edition.

Controls used in this application

private System.Windows.Forms.Button btnBrowseImg;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
public System.Windows.Forms.PictureBox pictureBox1;

Namespaces used in this application

using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Drawing;

Solution with Code

A struct-declaration consists of an optional set of attributes, followed by an optional set of struct-modifiers, followed by the keyword struct and an identifier that names the struct, followed by an optional struct-interfaces specification, followed by a struct-body, optionally followed by a semicolon. And the Serializable keyword indicates that a class can be serialized. This class cannot be inherited.

public struct ShellExecuteInfo
    public int Size;
    public uint Mask;
    public IntPtr hwnd;
    public string Verb;
    public string File;
    public string Parameters;
    public string Directory;
    public uint Show;
    public IntPtr InstApp;
    public IntPtr IDList;
    public string Class;
    public IntPtr hkeyClass;
    public uint HotKey;
    public IntPtr Icon;
    public IntPtr Monitor;

// Code For OpenWithDialog Box
[DllImport("shell32.dll", SetLastError = true)]
extern public static bool 
       ShellExecuteEx(ref ShellExecuteInfo lpExecInfo);

public const uint SW_NORMAL = 1;

static void OpenAs(string file)
    ShellExecuteInfo sei = new ShellExecuteInfo();
    sei.Size = Marshal.SizeOf(sei);
    sei.Verb = "openas";
    sei.File = file;
    sei.Show = SW_NORMAL;
    if (!ShellExecuteEx(ref sei))
        throw new System.ComponentModel.Win32Exception();

How the sample works

Run the sample code and click on the Browse button and select the image you want to view in the viewer and then click OK. The image will be shown in the image viewer tool. Then if you want to view the same image in other image handling samples like Photoshop, ImageReady, etc, you just click on that image; the Open with dialog box will open, and in that you can select any tool you like to use. This is how the sample code works.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Swordface
Australia Australia
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionWindows 7 Pin
Sammed15-Jan-14 20:53
memberSammed15-Jan-14 20:53 
QuestionDoes this code run on windows 8 ? Pin
aliascodeproject29-May-13 2:47
memberaliascodeproject29-May-13 2:47 
Question64 bit mechine [modified] Pin
dudyala15-Jul-11 21:50
memberdudyala15-Jul-11 21:50 
GeneralThanks! Pin
MJel1723-May-11 2:43
memberMJel1723-May-11 2:43 
QuestionThank you, and How to add an Exited event to it or get which button(OK or Cancel) user clicked in the select program window? Pin
zhyale11-Nov-10 22:47
memberzhyale11-Nov-10 22:47 
GeneralThank You Pin
mafaz32112-Feb-10 18:32
membermafaz32112-Feb-10 18:32 
GeneralThanks Pin
wehadfun10-Sep-09 18:05
memberwehadfun10-Sep-09 18:05 
GeneralGreat, thank you Pin
r2d2ro8-Mar-09 23:37
memberr2d2ro8-Mar-09 23:37 
GeneralVista fix Pin
brucerer25-Feb-09 12:07
memberbrucerer25-Feb-09 12:07 
QuestionRe: Vista fix Pin
zozo7127-May-09 23:27
memberzozo7127-May-09 23:27 
AnswerRe: Vista fix Pin
alexander20048-Mar-11 22:44
memberalexander20048-Mar-11 22:44 
GeneralGet the location of choosen app Pin
pedersen-roxen15-Feb-09 21:31
memberpedersen-roxen15-Feb-09 21:31 
GeneralSimplified, Three Lines of Code [modified] Pin
DefiantBehavior19-Dec-08 9:44
memberDefiantBehavior19-Dec-08 9:44 
GeneralRe: Simplified, Three Lines of Code Pin
kunluzhu22-Jul-10 20:43
memberkunluzhu22-Jul-10 20:43 
GeneralThe above code is not working on Windows Vista. Pin
Member 346999524-Sep-08 19:44
memberMember 346999524-Sep-08 19:44 
The above code is not working on Windows Vista. Plz help me, If you have any idea. Here it shows an error "<b>This file does not have a problem associated with it for performing this action. Create an association in the set accociations control panel</b>"
Questionopen with dialogbox Pin
skvani15-Jul-08 20:17
memberskvani15-Jul-08 20:17 
GeneralGreat job Pin
Member 37098454-Feb-08 0:00
memberMember 37098454-Feb-08 0:00 
GeneralThanks Pin
Matthew Cuba5-Sep-07 4:21
memberMatthew Cuba5-Sep-07 4:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151002.1 | Last Updated 17 Feb 2006
Article Copyright 2006 by vivekthangaswamy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid