Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 513
Vimal Sharma Avatar asked Nov 10 '22 11:11

Vimal Sharma


1 Answers

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.

like image 50
LInsoDeTeh Avatar answered Nov 14 '22 23:11

LInsoDeTeh