Requested section not present in activation context

michelek

Newcomer
Joined
Aug 31, 2004
Messages
10
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);
}
}
 
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:
C#:
[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:
C#:
ProcessStartInfo info = new ProcessStartInfo(@"C:\Program Files\Internet Explorer\iexplore.exe");
info.Arguments = "http://www.google.com";
info.UseShellExecute = false;
Process.Start(info);

-ner
 
Back
Top