2012-03-28 102 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