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
createprocesscannot elevation, create process.shellexecute1 knows how launch consent.exe, , consent.exe 1 checks group policy options.
note: code released public domain. no attribution required.
Comments
Post a Comment