views:

427

answers:

3

Hello everyone,

I am using VSTS 2008 + C# + .Net 2.0. And I want to invoke IE to open an html file located under pages sub-folder of my current executable.

Since my program may run under Windows Vista, I want to invoke IE under administrative permissions (Run As Administrator).

Any code to make reference? I am especially interested in how to write portable code, which works on both Windows Vista and Windows XP (I think Windows XP does not have function like Run As Administrator)

EDIT 1:

I am using the following code, but there is no UAC (User Access Control) prompt message box opened up to let me select Continue to run with Administrator. Any ideas what is wrong?

    ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
    startInfo.Verb = "RunAs";
    startInfo.Arguments = @"C:\test\default.html";
    Process.Start(startInfo);

thanks in advance, Geroge

+2  A: 
System.Diagnostics.Process.Start("iexplore.exe", @"C:\mypage.html");
John T
where in your code you start with "Run as Administrator"?
George2
Hi John, please refer to edit 1 section of my original post. The current issue is there is no UAC (User Access Control) prompt message box opened up to let me select Continue to run with Administrator. Any ideas what is wrong?
George2
+3  A: 

For working with relative paths, give a look to the GetFullPath method.

string fullPath = Path.Combine(Path.GetFullPath(@".\dir\dir2"), "file.html");
System.Diagnostics.Process.Start("iexplore.exe", fullPath);
CMS
Hi CMS, where in your code you start with "Run as Administrator"?
George2
Hi CMS, please refer to edit 1 section of my original post. The current issue is there is no UAC (User Access Control) prompt message box opened up to let me select Continue to run with Administrator. Any ideas what is wrong?
George2
+2  A: 
using System.Diagnostics;

Process the_process = new Process();
the_process.StartInfo.FileName = "iexplore.exe";
the_process.StartInfo.Verb = "runas";
the_process.StartInfo.Arguments = "myfile.html";
the_process.Start();

the verb "runas" will make it prompt the UAC And run under administrative priviliges.

you can run that code underboth vista and XP. It will yield same effect. As for the file which you want to open, you can pass it as the argument to iexplore.exe by using the_process.arguments = "

Anirudh Goel
Class Process has no constructor which accepts one string type parameter.
George2
Hi Anirudh, please refer to edit 1 section of my original post. The current issue is there is no UAC (User Access Control) prompt message box opened up to let me select Continue to run with Administrator. Any ideas what is wrong?
George2
sorry my bad that i put in an argument for the constructor. are you running the visual studio in adminstrator? I just tried it on machine and it ran properly. And then it occured to me, when has ever UAC prompt appeared to run iexplore?
Anirudh Goel
run it with some other applciation which needs administrator rights, it will work!
Anirudh Goel
Thanks Anirudh, it works!
George2
your welcome george. Do let me know if any further trouble comes up.
Anirudh Goel