如何使用c#的net用户

Ram*_*Ram 2 .net c# shell command

我正在尝试使用c#的net用户

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;
Run Code Online (Sandbox Code Playgroud)

当我执行代码Win32异常时发生消息系统无法找到指定的文件

例外情况如下

在位于D:\ GetUserFromAD\GetUserFromAD\Form1.cs中的GetUserFromAD.Form1.GetInformation(String id)的System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo):GetUserFromAD.Form1.button_Click(Object sender,EventArgs e)中的第25行在System.Windows的System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)的System.Windows.Forms.Control.OnClick(EventArgs e)中的D:\ Ram\MyC#\ GetUserFromAD\GetUserFromAD\Form1.cs:第35行System.Windows.Forms上System.Windows.Forms.ButtonBase.WndProc(Message&m)的System.Windows.Forms.Control.WndProc(Message&m)上的.Forms.Control.WmMouseUp(Message&m,MouseButtons按钮,Int32单击)系统中的System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)
位于System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)的.Button.WndProc(Message&m).
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.Uns中的Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)System.Windows.Forms.Application.ThreadContext.RunMessageLoop上的System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)中的afeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32 reason,Int32 pvLoopData)(Int32原因, ApplicationContext context)位于Microsoft.VisualStudio的System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)的D:\ Ram\MyC#\ GetUserFromAD\GetUserFromAD\Program.cs:第18行的GetUserFromAD.Program.Main()
中. System.Threading.ThreadHelper.ThreadStart()
中的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)中的HostingProcess.HostProc.RunUsersAssembly()

rav*_*ned 11

net是命令.从一user开始,一切都是命令论证.因此,您需要使用以下构造函数:

System.Diagnostics.ProcessStartInfo proccessStartInfo = new System.Diagnostics.ProcessStartInfo("net", "user " + id + " /domain");
Run Code Online (Sandbox Code Playgroud)

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

proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
Run Code Online (Sandbox Code Playgroud)