如何使用net用户与c# [英] How to use net user with c#

查看:168
本文介绍了如何使用net用户与c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用net用户与c#

I am trying to use net user with 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异常发生与消息系统找不到指定的文件

When the I execute the code Win32 exception occurs with message The system cannot find the file specified

例外情况如下:


$ b System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo
startInfo)GetUserFromAD.Form1.GetInformation(String id)in
D:\GetUserFromAD\GetUserFromAD\Form1.cs:line 25 at
GetUserFromAD.Form1.button_Click(Object sender,EventArgs e)in
D:\Ram\MyC#\GetUserFromAD\GetUserFromAD\Form1.cs:第35行
系统。 Windows.Forms.Control.OnClick(EventArgs e)at
System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)at
System.Windows.Forms.Control.WmMouseUp(Message& m,MouseButtons
按钮,Int32点击)at
System.Windows.Forms.Control.WndProc(Message& m)at
System.Windows.Forms.ButtonBase.WndProc(Message& m)at
System.Windows.Forms.Button.WndProc(Message& m)at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg ,IntPtr wparam,IntPtr lparam)at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

at
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(程序集,String [] args)

at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly )

System.Threading.ExecutionContext.Run(ExecutionContext
executionContext,ContextCallback callback,Object state)at
System.Threading.ThreadHelper.ThreadStart()

at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo) at GetUserFromAD.Form1.GetInformation(String id) in D:\GetUserFromAD\GetUserFromAD\Form1.cs:line 25 at GetUserFromAD.Form1.button_Click(Object sender, EventArgs e) in D:\Ram\MyC#\GetUserFromAD\GetUserFromAD\Form1.cs:line 35 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at GetUserFromAD.Program.Main() in D:\Ram\MyC#\GetUserFromAD\GetUserFromAD\Program.cs:line 18 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()


推荐答案

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

net is the command. Everything from user onwards are command arguments. As such, you'll need to use the following constructor:

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

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

In addition, in order to capture the standard output you'll need to set the following properties before you call proc.Start()

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

这篇关于如何使用net用户与c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆