Starting another process with elevation using different user credentials

前端 未结 5 881
旧巷少年郎
旧巷少年郎 2021-02-04 02:00

I\'m trying to start an elevated process from with a non-elevated process, but I also need to supply the username and password for a user with administrative credentials. I\'ve

5条回答
  •  孤城傲影
    2021-02-04 02:22

    According to the MSDN documentation:

    When UseShellExecute is false, you can start only executables by using the Process object.

    I noticed your declaration var proc = Process.Start(info); is not using Process as the class type.

    Also make sure parameter path is the fully qualified path to the executable. For instance, "c:\\directory\\contains\\process_to_be_started\\executable.exe"

    According to the MSDN documentation this is important:

    The WorkingDirectory property must be set if UserName and Password are provided. If the property is not set, the default working directory is %SYSTEMROOT%\system32.

    I would try below code to run target process with elevated privileges (with admin rights).

    ProcessStartInfo info = new ProcessStartInfo(path);
    
    info.UseShellExecute = false;
    info.UserName = username;
    info.Password = securePwd;
    info.Domain = "MyDomain";
    info.Verb = "runas";
    info.WorkingDirectory = "c:\\directory\\contains\\process_to_be_started"
    
    'var proc = Process.Start(info);
    
    Process proc = Process.Start(info);
    

提交回复
热议问题