Running CMD as administrator with an argument from C#

后端 未结 5 2177
一向
一向 2020-12-10 16:54

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

相关标签:
5条回答
  • 2020-12-10 17:40

    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.

    0 讨论(0)
  • 2020-12-10 17:43

    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.

    0 讨论(0)
  • 2020-12-10 17:47

    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.

    0 讨论(0)
  • 2020-12-10 17:49

    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#.

    enter image description here

    On application manifest file rename "asInvoker" as "requireAdministrator". At the end, application manifest file should look like:

    enter image description here

    Now, build your solution. Then you will be able to open all the apps as administrator rights.

    0 讨论(0)
  • 2020-12-10 17:50

    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
    
    0 讨论(0)
提交回复
热议问题