0

Very new to C#, creating a small exe that will launch on startup for users, giving them a few options, connect to a terminal server, administration (will be password protected..somehow :) ) and Exit.

Now administration I want to launch Task Manager as another user, so my idea was to call cmd.exe with runas and launch it. However, I'm not having much luck, my below code just launches cmd.exe but none of the arguments I specified with it.

private void btnAdministration_Click(object sender, EventArgs e)
{
    ProcessStartInfo processAdmin;
    processAdmin = new ProcessStartInfo();
    processAdmin.FileName = "C:\\Windows\\system32\\cmd.exe";
    processAdmin.Arguments = "runas /user:admin C:\\Windows\\System32\\taskmgr.exe";
    Process.Start(processAdmin);
}
1
  • From what I know, cmd.exe will not run their arguments as commands, you need to take stdin of the process. Commented Sep 14, 2013 at 17:06

2 Answers 2

1

Try that:

private void btnAdministration_Click(object sender, EventArgs e)
        {
            string password = "userpassword";
            SecureString secureString = new SecureString();

            foreach(char chr in password) secureString.AppendChar(chr);

            ProcessStartInfo processAdmin;
            processAdmin = new ProcessStartInfo();
            processAdmin.UseShellExecute = false;
            processAdmin.Password = secureString;
            processAdmin.UserName = "admin";
            processAdmin.FileName = "taskmgr.exe";
            processAdmin.WorkingDirectory = "C:\\Windows\\System32";
            Process.Start(processAdmin);

        }

What MSDN says about that:

Setting the Domain, UserName, and the Password properties in a ProcessStartInfo object is the recommended practice for starting a process with user credentials.

Note:

Putting the password on a string is not recommended as can be a security concern

Sign up to request clarification or add additional context in comments.

8 Comments

Did you received a InvalidOperationException ? If so, I modified try again
Well no, but how will it run if I cannot specify a password for that account I want to run it as?
Is there anyway we can get a prompt to enter the password, I would rather not embed the password in plaintext in my code.
Of course, you don't know how to ask user input!? Simple change the password variable
Ofc, I mean, to pass straight through to an encrypted string I can use in the processAdmin.Password call
|
0

Try to execute runas.exe

        ProcessStartInfo processAdmin;
        processAdmin = new ProcessStartInfo();
        processAdmin.UseShellExecute = false;
        processAdmin.FileName = "runas.exe"
        processAdmin.Arguments = "/user:Administrator \"c:\\Windows\\System32\\taskmgr.exe\""
        Process.Start(processAdmin);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.