How to run an exe as an admin from C# code? - c#

When I run dism /Online /Disable-Feature:Microsoft-Hyper-V-Allcommand from command prompt, It works fine.
But same I want to do from C# code. It does not work, Process exists with error code 11.
Process proc = new Process();
proc.StartInfo.FileName = "dism.exe";
proc.StartInfo.Arguments = "/Online /Disable-Feature:Microsoft-Hyper-V-All";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.Verb = "runas";
proc.Start();
proc.WaitForExit();
int exitCode = proc.ExitCode;
Basically I want to run the given command from C# code (with UAC) as it works with command prompt.

You can't. That is, you as a programmer, don't get to decide whether or not your code runs with administrative rights. However, you can inform the user that your code requires administrative rights and then ask to be granted those rights. How to do that is covered here.

try this method,
You'll want to modify the manifest that gets embedded in the program. This works on Visual Studio 2008 and higher: Project + Add New Item, select "Application Manifest File". Change the element to:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Related

Running un-elevated command from an elevated C# exe or vice versa

I have an elevated C# exe file that can run elevated commands using System.Diagnostics.Process just fine, however I need to run a specifically unelevated command - that being subst. I am not sure whether I should (or respectively could) make a secondary .exe file or .bat or something?
There is an alternative, as there is only one command that requires admin privileges to run - that being a WSL mount command. Can I make the C# script unelevated and just elevate that one specific command, or vice versa?
I've tried using runas /trustlevel:0x20000, but that throws an access denied (even though the directory being accessed by the SUBST command is \\WSL$\
As for the alternative solution I've tried the following code:
startInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = $"/c powershell -command wsl --mount \\.\PHYSICALDRIVE1 --partition 2;pause"
startInfo.Verb = "runas";
Process proc = Process.Start(startInfo)
proc.WaitForExit();
That opens up an UAC prompt, opens up an elevated powershell Window, but for some reason doesn't recognize "WSL" as a command, even when using it's absolute path.
Fixing either method would fix the issue.

run command line (cmd) that require "Security Message"

I want to run the command line pnputil in c# program. The program needs to install USB driver.
I know how to run cmd in c# program, but I have a different problem:
The driver that I want to install does't have permission of windows.
If I Install the driver via the "device manager->update driver" and choose the path of the driver, I get "Security Message" from windows that "windows can't verify the publisher of this drive software" and let me choose if install the driver or not (of course, if I choose to install - the installing succeeds).
If I run the command from the cmd pnputil -a <path_name_inf> I get this message too and I can install the driver.
But when I try to run the command via c# program - the program runs but the driver is not installed (I also don't get this message).
my code in c#:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = #"/C Pnputil -a <path_name_inf>";
process.StartInfo = startInfo;
process.Start();
How can I do that?
You could try to run the cmd using the runas verb:
startInfo.Verb = "runas";
startInfo.UseShellExecute = true;
This parameter causes a privileges elevation. The same thing could be reached when you use "Run as administrator" in explorer.
Your questions is more towards installing unsigned drivers.
Can you please try following steps:
Open a command prompt as an admin and type:
bcdedit -set TESTSIGNING ON
Please refer MSDN link for more infromation on Test Signing. Making Test signing may put a watermark as TEST in desktop waterpaper
I know this is old, but I wanted to share my solution in case it can help someone else.
My specific scenario is that I wanted to add an unsigned driver package to the driver store on Windows 7 Home Premium 64-bit.
Like the OP, this worked as expected (meaning I received the security warning and the driver package was added to the driver store) if I executed pnputil -a <path_to_inf> using a command prompt using "Run as administrator".
However, if I tried to call pnputil from within C#, I was never able to get the security warning and the driver package was not added to the driver store. I had tried using various options (i.e. Verb = "runas", UseShellExecute = true, etc) with the FileName set as "pnputil" or "cmd".
Ultimately, what worked for me was to create a simple batch file that contained the following:
%windir%\sysnative\pnputil /a <path_to_inf>
My C# app would then call this batch file as follows:
Process proc = new Process();
proc.StartInfo.FileName = "<path_to_bat_file>";
proc.StartInfo.Verb = "runas";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
The user would first be prompted because I asked to run with elevated permission followed by the expected security warning. The driver package would then be added to the driver store as expected.
If this does not work as expected, you can add "pause" (without quotes) on a new line after the last command in the batch file and remove the WindowStyle and CreateNoWindow lines from the C# code to see what is happening on the command prompt.

Start Process as Administrator, without UseShellExecute?

So here is my issue:
I have start a .bin file, which is just a .exe, renamed to .bin, with administrator privileges.
Here is what I have:
PSI.FileName = "Client.bin";
PSI.WorkingDirectory = Directory.GetCurrentDirectory();
PSI.UseShellExecute = false;
PSI.Verb = "runas";
Process.Start(PSI);
I also have the requireAdministrator set in the manifest.
I have to set UseShellExecute to false, seeing is that is the only way I can find that starts the .bin as a .exe. However, according Here, runas, and the manifest only are used when UseShellExecute is set to true.
Question: How to start a non .exe process, with elevated privileges?
Have a parent process which is a executable with administrative privilege & then launch the ".bin" file from it.
According to this answer this is not possible to run .bin file with elevation directly.
At least you can run your own .exe with runas (and ShellExecute = true) and order it with commandline to run .tmp (without ShellExecute, but the process is already elevated) and die. But this is the "last chance" solution.

How to run .Bat file in asp.net,IIS

How to start XXX.bat file in IIS? I want to run this file..while i am executing in my development PC its perfectly running. But While I deployed in IIS its working and not executing .bat file
So please guide me how to do...My Code as follows
\\
string exefile = ConfigurationManager.AppSettings["exefile"]; //exefile="D:\\XX\\xxx.bat"
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(exefile);
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
\\
I would check security.
The user that "hosts" the IIS proccess does likely not have the rights to run exeute files outside the IIS folder.
Also posting a actual error message would be helpfull. On a side note if its Exepction is of type UnauthorizedAccessException then its very likely what i describe above.
I had this problem and for me it was the location of the bat file.
It had placed the bat file in it's own folder under the solution in Visual Studio but this meant IIS could not access it.
When I moved the bat file to the wwwroot folder everything worked as expected.

Elevating a process to run as admin doesn't work

Elevating a process to run as admin doesn't work. If I run that application from an elevated command prompt it runs fine. But my code below doesn't.
Process setupws = new Process();
setupws.StartInfo.FileName = #"setupws.exe";
setupws.StartInfo.Verb = "runas";
setupws.StartInfo.UseShellExecute = true;
setupws.Start();
setupws.WaitForExit();
The setupws.exe file runs fine, just not as an admin.
What am I doing wrong?
Thanks
PS. I've also used highestAvailable and requireAdministrator in my app.manifest file
You need to mark your installer as requestedExecutionLevel level=requireAdministrator in the manifest, see Create and Embed an Application Manifest (UAC).
PS. The requireAdministrator should be in the setupws.exe's manifest.
Have you ran your app as admin and tried it that way? You could do something like:
using System.Security.Permissions;
var mine = new EnvironmentPermission(PermissionState.Unrestricted);
mine.AddPathList(EnvironmentPermissionAccess.AllAccess, Environment.CurrentDirectory);

Categories