.NET启动进程具有较高的权 [英] .NET Start Process with higher rights
问题描述
我想通过一个C#应用程序,它获取只有用户权限调用执行管理员权限的程序。
code
的ProcessStartInfo磅;
尝试
{
PSI =新的ProcessStartInfo(@WINZIP32.EXE);
psi.UseShellExecute = FALSE;
SecureString的PW =新SecureString的();
pw.AppendChar('P');
pw.AppendChar('一个');
pw.AppendChar('S');
pw.AppendChar('S');
pw.AppendChar(W);
pw.AppendChar('O');
pw.AppendChar('R');
pw.AppendChar('D');
psi.Password = PW;
psi.UserName =管理员;
的Process.Start(psi)的;
}
赶上(例外前)
{
的MessageBox.show(ex.Message);
}
它启动WinZip的,但只有用户权限。有什么我做错了或者是它甚至有可能启动一个进程具有更高的权限?
谢谢!
编辑: 这是问题背后的原因,也许这有助于了解我的实际需要。
我用的WinZip例如获得的总体思路有什么不正确与我的code。实际的问题是,我们公司使用2个版本的程序。但在此之前启动任何你需要用REGSVR32(管理员权限),导入一个DLL文件的版本。现在我想编写一个让用户选择的版本,导入DLL并启动正确的应用程序。
您需要设置<一个href="http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.useshellexecute.aspx">ProcessStartInfo.UseShellExecute以真
和<一href="http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.verb.aspx">ProcessStartInfo.Verb以运行方式
:
工艺过程= NULL;
的ProcessStartInfo的ProcessStartInfo =新的ProcessStartInfo();
processStartInfo.FileName =WINZIP32.EXE;
processStartInfo.Verb =运行方式;
processStartInfo.WindowStyle = ProcessWindowStyle.Normal;
processStartInfo.UseShellExecute =真;
流程=的Process.Start(的ProcessStartInfo);
这会导致应用程序作为管理员身份运行。 UAC将然而提示用户进行确认。如果这是不可取的,那么你就需要添加一个清单永久提升主机进程的privilages。
I am trying to execute a program with admin rights through a C# application, which gets invoked with user rights only.
Code
ProcessStartInfo psi;
try
{
psi = new ProcessStartInfo(@"WINZIP32.EXE");
psi.UseShellExecute = false;
SecureString pw = new SecureString();
pw.AppendChar('p');
pw.AppendChar('a');
pw.AppendChar('s');
pw.AppendChar('s');
pw.AppendChar('w');
pw.AppendChar('o');
pw.AppendChar('r');
pw.AppendChar('d');
psi.Password = pw;
psi.UserName = "administrator";
Process.Start(psi);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
It does start winzip, but only with user rights. Is there something I am doing wrong or is it even possible to start a process with higher rights?
thank you!
Edit: Here is the reason behind the question, maybe it helps to understand what i actually need.
I used winzip for example to get a general idea what's incorrect with my code. The actual problem is, our company uses 2 versions of a program. But before you start any of the versions you need to import a dll file with regsvr32 (with admin rights). Now I would like to write a program that let the user select the version, import the dll and starts the correct application.
You need to set ProcessStartInfo.UseShellExecute to true
and ProcessStartInfo.Verb to runas
:
Process process = null;
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = "WINZIP32.EXE";
processStartInfo.Verb = "runas";
processStartInfo.WindowStyle = ProcessWindowStyle.Normal;
processStartInfo.UseShellExecute = true;
process = Process.Start(processStartInfo);
This will cause the application to run as the administrator. UAC will however prompt the user to confirm. If this is not desirable then you'll need to add a manifest to permanently elevate the host process privilages.
这篇关于.NET启动进程具有较高的权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!