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 following 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With