我有这种情况的一个问题: 我做了2个节目:C#程序如何以管理员权限运行另一个程序?
第一个只是打印输出称与管理provileges推出与否,和第二个,执行第一条程序拥有管理员权限且不使用UAC。麻烦的是,第二个程序无法启动具有管理员权限的第一个程序,我不知道为什么。 这是我的代码:
的第一个程序的代码:
// This only prints if you start as administrator or not.
bool isElevated;
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);
Console.WriteLine("I got admin privileges?: "+isElevated);
代码的第二方案:
// This execute the first program with admin privileges without UAC
string username = "myuser";
SecureString userpass = new SecureString();
userpass.AppendChar('m');
userpass.AppendChar('y');
userpass.AppendChar('p');
userpass.AppendChar('a');
userpass.AppendChar('s');
userpass.AppendChar('s');
Process program = new Process();
program.StartInfo.UserName = username;
program.StartInfo.Password = userpass;
program.StartInfo.FileName = "Path/First_program.exe";
program.StartInfo.UseShellExecute = false;
program.Start();
PD:我不希望用户打开UAC,这就是为什么我已经插入用户名和密码。 在此先感谢。
据我所知,你不能以这种方式绕开UAC。如果正在运行的程序没有管理权限,则被调用的程序需要请求将触发UAC的管理权限。 – itsme86
你有没有尝试使密码'SecureString'只读? –
@ BenVoigt,“SecureString”工作正常,因为当我写一个错误的密码它会引发一个异常。 – suffuko