Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am new to C# and I'm trying to launch the default internet browser. I'm getting the error message "The requested section was not present in the activation context" when I run the setup application. Does anyone know what this error message means? And does anyone know of a good C# book with good examples and talks about setup and deployment? Here's the code that I've placed after the InitializeComponent function:

 

public override void Install(IDictionary stateSaver)

{

base.Install (stateSaver);

 

string target = "http://www.microsoft.com";

 

try

{

System.Diagnostics.Process.Start(target);

}

catch

(System.ComponentModel.Win32Exception noBrowser)

{

if(noBrowser.ErrorCode==-2147467259)

MessageBox.Show(noBrowser.Message);

}

catch (System.Exception other)

{

MessageBox.Show(other.Message);

}

}

  • *Experts*
Posted

From doing a little digging, I'm guessing that your process is running as a multi-threaded apartment? Meaning, your Main has the MTAThread attribute:

[MTAThread]
static void Main() 
{
...
}

 

I believe a lack of the attribute will default to MTAThread, vs. STAThread.

 

Here's a summary of the problem:

First, if you use Process.Start("http://..."), you're basically telling it to start a process using the Shell. This means, go through the Windows shell to handle all protocol prefixes like http and open default applications for extensions. So if you tried to run Process.Start("readme.txt") it would open Notepad.

 

You can create a ProcessStartInfo object to pass to Process.Start. If you create one, you'll see the UseShellExecute property. This is basically being set to False for you when you only pass a string to Process.Start.

 

The problem is this: in a Mult-Threaded apartment model you cannot use the shell. Not really sure why, but it's very clear about this point in the docs :)

 

So why not set UseShellExecute to false? You can, but then you can't give it a string like "http://...", you MUST provide a valid executable. If you want, you could read the registry and get the default browser path/filename and use that. I'm not sure where to find this, but here's a code snippet on how to use it:

ProcessStartInfo info = new ProcessStartInfo(@"C:\Program Files\Internet Explorer\iexplore.exe");
info.Arguments = "http://www.google.com";
info.UseShellExecute = false;
Process.Start(info);

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...