C# Start process as adminstrator - does not work

29 Views Asked by At

In my C# application i want to start a process (VSCode) in admin mode - as if i would right click the icon and choose "Run as administrator" - without prompting for a password.

I read several threads, and all are about the same: Using ProcessStartInfo.Verb = "runas" combined with .UseShellExecute = false, .CreateNoWindow = true and .RedirectStandardOutput = true. But it does not work. When i watch the process in the Task Manager, in the Details tab the "Elevated" column ist empty - i expect Elevated=Yes for a process in admin mode.

Here ist my code:

string regKeyVSCode = Tools.GetRegistryKeyRoot(@"Applications\Code.exe\shell\open\command", "");
string[] commandParts = regKeyVSCode.Split(@"""");                        
ProcessStartInfo startInfo = new ProcessStartInfo(commandParts[1], GlobalConfig.MySWPackageDataProvider.SelectedPackagePath);
startInfo.Verb = "runas";
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
//startInfo.ErrorDialog = true;
//startInfo.UserName = "admin";
Process.Start(startInfo);

If i set UseShellExecute to true, the username/password dialog appears, but that's not what i want. I want to start in admin mode without entering credentials.

Can anybody please tell me what i'm making wrong and how to achieve what i want?

Thank you very much in advance Best regards Emanuel

0

There are 0 best solutions below