c# - Run process as administrator from a non-admin application -


from application not being run administrator, have following code:

processstartinfo proc = new processstartinfo(); proc.windowstyle = processwindowstyle.normal; proc.filename = myexepath; proc.createnowindow = false; proc.useshellexecute = false; proc.verb = "runas"; 

when call process.start(proc), not pop asking permission run administrator, , exe not run administrator.

i tried adding app.manifest executable found @ myexepath, , updated requestedexecutionlevel to

<requestedexecutionlevel level="requireadministrator" uiaccess="false" /> 

with updated app.manifest, on process.start(proc) call, exception, "the requested operation requires elevation."

why isn't .verb action not setting administrator privileges?

i testing on windows server 2008 r2 standard.

you must use shellexecute. shellexecute api knows how launch consent.exe in order elevate.

sample (.net) source code

in c#, way call shellexecute use process.start along useshellexecute = true:

private void button1_click(object sender, eventargs e) {    processstartinfo info = new processstartinfo(@"c:\windows\notepad.exe");    info.useshellexecute = true;    info.verb = "runas";    process.start(info); } 

if want developer, can catch when user clicked no:

private void button1_click(object sender, eventargs e) {    const int error_cancelled = 1223; //the operation canceled user.     processstartinfo info = new processstartinfo(@"c:\windows\notepad.exe");    info.useshellexecute = true;    info.verb = "runas";    try    {       process.start(info);    }    catch (win32exception ex)    {       if (ex.nativeerrorcode == error_cancelled)          messagebox.show("why no select yes?");       else          throw;    } } 

bonus watching

  • uac - what. how. why.. architecture of uac, explaining createprocess cannot elevation, create process. shellexecute 1 knows how launch consent.exe, , consent.exe 1 checks group policy options.

note: code released public domain. no attribution required.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -