Possible 开发者_开发技巧Duplicate:
Elevating process privilege programatically?
I'd like to build a console application in .NET that simply runs another program with elevated rights. That way I can point it to, say, an install exe for Flash, send a shortcut to the user pointing to my 'runasadmin' exe, and the user can update Flash.
The credentials and the file name to run will be hidden from the user (within the compiled exe)
Is this secure, of course not. But the thought of hitting hundreds of users' workstations is making us look at alternatives :)
Is this feasible? Anyone have experience in this area?
Just so you know, Microsoft already provides a command line utility to run programs as a different user, including administrator: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/runas.mspx?mfr=true
You could create a shortcut that executes RunAs, with the appropriate command line parameters and your program would execute, in one click.
Try the Windows API call "CreateProcessWithLogonW" - I've used this from .Net with much success.
It sounds like you're in an corporate IT department, in which case I imagine you can create AD accounts that have admin privileges on users' machines. Once you've done that you can create a .NET app that P/Invokes to CreateProcessAsUser to launch the required install process under an admin account.
Some info on how to P/Invoke this method from .NET can be found here
Having said that, rolling out software to multiple users machines is a common requirement and there are a number of products available that will do this for you.
System.Diagnostics.ProcessStartInfo processStartInfo = new System.Diagnostics.ProcessStartInfo("path", "args");
processStartInfo.Verb = "runas";
using (System.Diagnostics.Process process = new System.Diagnostics.Process())
{
process.StartInfo = processStartInfo;
process.Start();
process.WaitForExit();
}
精彩评论