Start Process with administrator right in C#

后端 未结 5 1006
醉酒成梦
醉酒成梦 2020-12-06 07:04

I have to start a command line program with System.Diagnostics.Process.Start() and run it as Administrator.

This action will also be run by a Scheduled Task every da

5条回答
  •  失恋的感觉
    2020-12-06 07:30

    A better secure option to run a process with login and password is use the SecureString class to encrypt the password. Here a sample of code:

    string pass = "yourpass";
    string name ="login";
    SecureString str;
    ProcessStartInfo startInfo = new ProcessStartInfo();
    char[] chArray = pass.ToCharArray();
    fixed (char* chRef = chArray)
    {
        str = new SecureString(chRef, chArray.Length);
    }
    startInfo.Password = str;
    startInfo.UserName = name;
    Process.Start(startInfo);
    

    You must allow unsafe code in your project properties.

    Hope this help.

提交回复
热议问题