从 C# 执行网络用户命令

Execute net user command from C#

我想从 C# 执行网络用户命令。

下面是我的代码:-

         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();

当我 运行 程序时,命令提示符 window 在管理模式下打开并显示以下结果:-

这条命令的语法是:

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


C:\Windows\system32>

当我 运行 像 cd/ 、 dir e.tc 这样的简单命令时。 运行没问题。

我猜你的 Arguments 字符串末尾的转义反斜杠会杀死你命令的 /add 参数

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

将是字符串

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

所以尽量去掉末尾的\\。