I want to run cmd.exe
as administrator with arguments from C# in order to prevent a UAC popup. This is necessary in order to use it as an automated installation
There is at least one problem with your command, this line:
Arguments = "/user:Administrator cmd /K " + command
Should be:
Arguments = "/user:Administrator \"cmd /K " + command + "\""
Also, this won't work as a fully automated process, because it will ask for the Administrator's password which in Windows Vista and newer is not known.
Two solutions: First, you may the user's appdata directory. This will save you from needing admin privileges in the first place. (in a more general approach - think carefully if you really need those privileges)
Another solution us creating a Windows service which would have those privileges. The first installation of that service would require admin privileges, but after that, you can delegate your work to that service.
The second solution is a potentially security breach - so you'll have to think about what that service would be able to do carefully.
The UAC will pop up depending on the user settings in "User Account Control Settings." A program cannot bypass that. Only if the user has settings of "Never Notify" will your program do what you are trying to do.
Those who could not find the solution to their problems i have found this solution for me: On the solution file, choose
Add => New item => Application manifest file
then open it on C#.
On application manifest file rename "asInvoker" as "requireAdministrator". At the end, application manifest file should look like:
Now, build your solution. Then you will be able to open all the apps as administrator rights.
I have been using this code:
string[] commands = File.ReadAllLines(commandFile);
foreach (string command in commands)
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
//startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.WorkingDirectory = @"C:\Windows\System32";
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/user:Administrator \"cmd /K " + command + "\"";
process.StartInfo = startInfo;
process.Start();
}
As you can see: Trying this code from 'Run' in VS will not give admin, But if you compile this program and run it externally as admin it will. I used this batch file to test privilege level.
@echo off
goto check_Permissions
:check_Permissions
echo Administrative permissions required. Detecting permissions...
net session >nul 2>&1
if %errorLevel% == 0 (
echo Success: Administrative permissions confirmed.
) else (
echo Failure: Current permissions inadequate.
)
pause >nul