Click here to Skip to main content
6,595,444 members and growing! (19,718 online)
Email Password   helpLost your password?
Desktop Development » Shell and IE programming » Shell Programming     Intermediate License: The Code Project Open License (CPOL)

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

By vivekthangaswamy

This article illustrates calling the Open With dialog box from your application, using C#.
C#, Windows, .NET 2.0VS2005, Dev
Posted:17 Feb 2006
Views:49,142
Bookmarked:28 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
27 votes for this article.
Popularity: 6.16 Rating: 4.31 out of 5

1

2
3 votes, 11.1%
3
5 votes, 18.5%
4
19 votes, 70.4%
5

Abstract

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.

// 

[Serializable]
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.

References

License

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

About the Author

vivekthangaswamy


Member

Occupation: Architect
Location: India India

Other popular Shell and IE programming articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralThanks Pinmemberwehadfun19:05 10 Sep '09  
GeneralGreat, thank you Pinmemberr2d2ro0:37 9 Mar '09  
GeneralVista fix Pinmemberbrucerer13:07 25 Feb '09  
QuestionRe: Vista fix Pinmemberzozo710:27 28 May '09  
GeneralGet the location of choosen app Pinmemberpedersen-roxen22:31 15 Feb '09  
GeneralSimplified, Three Lines of Code [modified] PinmemberDefiantBehavior10:44 19 Dec '08  
GeneralThe above code is not working on Windows Vista. PinmemberMember 346999520:44 24 Sep '08  
Questionopen with dialogbox Pinmemberskvani21:17 15 Jul '08  
GeneralGreat job PinmemberMember 37098451:00 4 Feb '08  
GeneralThanks PinmemberMatthew Cuba5:21 5 Sep '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Feb 2006
Editor: Smitha Vijayan
Copyright 2006 by vivekthangaswamy
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project