简体   繁体   中英

Execute net user command from C#

I want to execute net user command from C#.

Below is my code:-

         ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd.exe");
         procStartInfo.UseShellExecute = true;
         procStartInfo.CreateNoWindow = true;
         procStartInfo.Verb = "runas";
         procStartInfo.Arguments = "/env /user:" + "Administrator" +  "cmd /K \"net user ABC Admin123# /add\\\"";

         ///command contains the command to be executed in cmd
         System.Diagnostics.Process proc = new System.Diagnostics.Process();
         proc.StartInfo = procStartInfo;
         proc.Start();

When i run the program the command prompt window open in administrative mode and show the folowing result:-

The syntax of this command is:

NET USER
[username [password | *] [options]] [/DOMAIN]
         username {password | *} /ADD [options] [/DOMAIN]
         username [/DELETE] [/DOMAIN]
         username [/TIMES:{times | ALL}]


C:\Windows\system32>

When i run simple commands like cd/ , dir e.tc. it runs fine.

I guess the escaped backslash at the end of your Arguments string kills the /add parameter of your command

"cmd /K \"net user ABC Admin123# /add\\\""

will be the string

cmd /K "net user ABC Admin123# /add\"

So try to remove the \\\\ at the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM