2012-03-28 100 views
0

我想用net user用户用C#如何使用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 STA如下

rtInfo)在GetUserFromAD.Form1.GetInformation(String id) D:\ GetUserFromAD \ GetUserFromAD \ Form1.cs:第25行 GetUserFromAD.Form1.button_Click(Object sender,EventArgs e) D:\ Ram \ MyC# \ GetUserFromAD \ GetUserFromAD \ Form1.cs:第35行 System.Windows.Forms.Control.OnClick(EventArgs e)at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)at System.Windows.Forms.Control .WmMouseUp(消息&米,MouseButtons 按钮,点击的Int32)在 System.Windows.Forms.Control.WndProc(消息& m)上 System.Windows.Forms.ButtonBase.WndProc(消息& m)上 系统。 Windows.Forms.Button.WndProc(消息& m)上 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&米)
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr的的HWND, 的Int32味精,IntPtr的WPARAM,IntPtr的LPARAM)在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG & MSG)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(的Int32 dwComponentID,的Int32原因,的Int32 pvLoopData)在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext conte xt) D:\ Ram \ MyC#\ GetUserFromAD \ GetUserFromAD \ Program.cs中的GetUserFromAD.Program.Main() System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext上下文):line 18在 System.AppDomain._nExecuteAssembly(大会组件,字串[] args)
在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在System.Threading.ExecutionContext.Run(的ExecutionContext 的ExecutionContext,ContextCallback回调,对象状态)在 System.Threading.ThreadHelper.ThreadStart()

+0

我不知道确切的原因,我可以说解决方法是将该语句放在批处理脚本中,并将该批处理作为参数传递给ProcessStartInfo API。 – Zenwalker 2012-03-28 10:39:01

+0

网络使用的更好实践:http://stackoverflow.com/questions/8919/looking-for-best-practice-for-doing-a-net-use-in-c-sharp – Kiquenet 2013-05-13 06:34:45

回答

6

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

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

此外,为了捕捉到标准输出,您需要设置下列属性调用proc.Start()

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

也许更好的网络使用实践:http://stackoverflow.com/questions/8919/looking-for-best-practice-for-doing-a-net-use-in-c-sharp – Kiquenet 2013-05-13 06:35:24

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

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

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

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

+1

尽管最好指定只要命令在用户的路径上,就不是严格必要的。 – raveturned 2012-03-28 10:50:40