【问题标题】:How to use net user with c#如何在 C# 中使用网络用户
【发布时间】:2012-03-28 10:34:42
【问题描述】:

我正在尝试将 net user 与 c# 一起使用

System.Diagnostics.ProcessStartInfo proccessStartInfo = new System.Diagnostics.ProcessStartInfo("net user " + id + " /domain");

proccessStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process {StartInfo = proccessStartInfo};
proc.Start();

string result = proc.StandardOutput.ReadToEnd();
textBoxOp.Text = result;

当我执行代码时出现Win32异常并显示消息系统找不到指定的文件

异常详情如下

在 System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo) 在 GetUserFromAD.Form1.GetInformation(String id) 中 D:\GetUserFromAD\GetUserFromAD\Form1.cs:第 25 行 GetUserFromAD.Form1.button_Click(Object sender, EventArgs e) in D:\Ram\MyC#\GetUserFromAD\GetUserFromAD\Form1.cs:第 35 行 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,鼠标按钮 按钮,Int32 点击)在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ButtonBase.WndProc(Message& m) 在 System.Windows.Forms.Button.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg,IntPtr wparam,IntPtr lparam)在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32 原因,Int32 pvLoopData)在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文)在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文)在 GetUserFromAD.Program.Main() 在 D:\Ram\MyC#\GetUserFromAD\GetUserFromAD\Program.cs:第 18 行 System.AppDomain._nExecuteAssembly(Assembly 程序集,String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态)在 System.Threading.ThreadHelper.ThreadStart()

【问题讨论】:

  • 我不知道确切的原因,我可以说的解决方法是将该语句放在批处理脚本中并将该批处理脚本作为参数传递给 ProcessStartInfo API。
  • 网络使用的更好做法:stackoverflow.com/questions/8919/…

标签: c# .net shell command


【解决方案1】:

net 是命令。从user 开始的所有内容都是命令参数。因此,您需要使用以下构造函数:

System.Diagnostics.ProcessStartInfo proccessStartInfo = new System.Diagnostics.ProcessStartInfo("net", "user " + id + " /domain");

此外,为了捕获标准输出,您需要在调用proc.Start()之前设置以下属性

proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;

【讨论】:

【解决方案2】:

您需要指定位于系统目录(即 windows\system32)中的 net.exe 的路径。例如,

var proccessStartInfo = new System.Diagnostics.ProcessStartInfo(
    Path.Combine(Environment.SystemDirectory, "net.exe"), "user " + id + " /domain");

另请注意,命令行参数作为第二个参数传递。

【讨论】:

  • 虽然最好指定您要使用的命令的路径,但只要命令在用户路径上,这不是绝对必要的。
【解决方案3】:
 System.Diagnostics.ProcessStartInfo proccessStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "net user " + id + "/domain");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    • 2018-04-28
    相关资源
    最近更新 更多