views:

422

answers:

2

Elevating a process to run as admin doesn't work. If I run that application from an elevated command prompt it runs fine. But my code below doesn't.

Process setupws = new Process();
setupws.StartInfo.FileName = @"setupws.exe";
setupws.StartInfo.Verb = "runas";
setupws.StartInfo.UseShellExecute = true;
setupws.Start();
setupws.WaitForExit();

The setupws.exe file runs fine, just not as an admin.

What am I doing wrong?

Thanks

PS. I've also used highestAvailable and requireAdministrator in my app.manifest file

+1  A: 

You need to mark your installer as requestedExecutionLevel level=requireAdministrator in the manifest, see Create and Embed an Application Manifest (UAC).

PS. The requireAdministrator should be in the setupws.exe's manifest.

Remus Rusanu
My manifest does have requestedExecutionLevel level="requireAdministrator" set. Also, my setupws.exe file is not using a manifest file, and its not mine so it probably won't ever. But that's what I thought the "runas" was for.
JimDel
It really should be a setupsw manifest... Anyway, you must also set the ErrorDialog and ErrorDialogParentHandle on the ProcessInfo, see http://msdn.microsoft.com/en-us/library/bb756922.aspx, because some HWND must own the elevation UI prompt dialog.
Remus Rusanu
+1  A: 

Have you ran your app as admin and tried it that way? You could do something like:

using System.Security.Permissions;

var mine = new EnvironmentPermission(PermissionState.Unrestricted);
mine.AddPathList(EnvironmentPermissionAccess.AllAccess, Environment.CurrentDirectory);
Michael