Click here to Skip to main content
15,886,104 members
Articles / Programming Languages / Visual Basic
Article

What About ProcessStartInfo.ErrorDialog Property? Or Another Way to Show the "Open With" Dialog

Rate me:
Please Sign up or sign in to vote.
4.19/5 (6 votes)
5 Jan 2007CPOL2 min read 26.6K   12   2
This article describes the use of the ErrorDialog property

Introduction

After several months of being a member on The Code Project, I decided to write my first article. It is about (in my opinion) a badly described and misunderstood property in the .NET Framework called ErrorDialog which is part of the System.Diagnostics.ProcessStartInfo class.

Background

I was writing an application similar to Windows Explorer which is more user-friendlier (hopefully) with some extra features and optimizations. Therefore I needed an implementation to open files from a ListView control. I used the ProcessStartInfo class to realize it.
However, when I tried to open files with extensions like *.bin, *.sys and so on, instead of the "Open With" dialog, only the Win32Exception appeared, which I "caught" first of all....

And There Is a Light At the End of the .NET Framework

... Then I looked for help on The Code Project. The solution I found was to send the SHELLEXECUTEINFO structure with the Verb field set to "openas" through the SHELLEXECUTEINFOEX method. Reasons for not using this solution were unnecessary Win32 call and trying always to use .NET first.

I tried out the "openas" value by setting it to the ProcessStartInfo.Verb property but it didn't work. So I disassembled the System.Diagnostics.Process.Start(ProcessStartInfo) method to its roots and found out that it uses the same way as described in that other article, but with one difference:
The type of the lpVerb field isn't a string but an IntPtr and .NET Framework converts ProcessStartInfo.Verb value to IntPtr type. :(

So I looked at the other properties. And here it comes: it is the ErrorDialog property which is responsible for showing "Open With" dialog!

The Code

Now, after a slightly longer solution, here is the "whole" code to solve the problem.
It is a part of my bigger application written in C#. This code is executed when the user double clicks a file item in the listview.

C#
void lvFiles_ItemActivate(object sender, EventArgs e)
{
    System.Diagnostics.ProcessStartInfo psi = 
    new System.Diagnostics.ProcessStartInfo();
    psi.FileName = this.lvFiles.SelectedItems[0].Name;
    psi.UseShellExecute = true;
    psi.ErrorDialog = true;
    try
    {
        System.Diagnostics.Process.Start(psi);
    }
    catch (Win32Exception ex)
    {
        MessageBox.Show(this, "Couldn't open the file!\n" + 
        ex.ToString(), "Explorer XTreme");
    }
    finally
    {
        psi = null;
    }
}

Here is the code "translated" for Visual Basic users.

VB.NET
Sub lvFiles_ItemActivate(ByVal sender As Object, ByVal e As EventArgs)
    Dim psi As System.Diagnostics.ProcessStartInfo = 
        New System.Diagnostics.ProcessStartInfo()
    psi.FileName = Me.lvFiles.SelectedItems(0).Name
    psi.UseShellExecute = True
    psi.ErrorDialog = True
    Try
        System.Diagnostics.Process.Start(psi)
    Catch ex As Win32Exception
        MessageBox.Show(Me, "Couldn't open the file!\n" + 
            ex.ToString(), "Explorer XTreme")
    Finally
        psi = Nothing
    End Try
End Sub

Points of Interest

First of all, I see that a direct implementation of the SHELLEXECUTEINFO class is more powerful and I think I certainly will use it in my application for other things (e.g. to show Windows File Properties dialog). But the way I showed you is easier for that kind of "problem", of course if you don't always want to let the user choose the application. Another thing I'm angry about is that the use of the Verb property is very limited. I think in .NET Framework documentation, it should be pointed out that this property shows "Open With" dialog, at least on Windows platforms.

OK, I think that's enough for my first article. Have fun with the code.

P.S.: Sorry if my English is not always correct.

History

  • 5th January, 2007: Initial post

License

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


Written By
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionread "verbs" and use one ... Pin
Member 1494700414-Dec-22 19:43
Member 1494700414-Dec-22 19:43 
GeneralErr Pin
M.Ardekanian26-Aug-07 23:10
M.Ardekanian26-Aug-07 23:10 

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

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