【问题标题】:Execute net user command from C#从 C# 执行 net user 命令
【发布时间】:2015-06-12 12:38:04
【问题描述】:

我想从 C# 执行 net user 命令。

下面是我的代码:-

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

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

这个命令的语法是:

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 等简单命令时。它运行良好。

【问题讨论】:

    标签: c# asp.net cmd window window-server


    【解决方案1】:

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

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

    将是字符串

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

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

    【讨论】:

      猜你喜欢
      • 2013-01-13
      • 2017-10-18
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多