2

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;

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

Details of exceptions are as follows

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()

Ram
  • 11,404
  • 15
  • 62
  • 93
  • I do not know the exact cause, work around i can say is put that statement in a batch script and pass that batch sript as argument to ProcessStartInfo API. – Zenwalker Mar 28 '12 at 10:39
  • Better practices for net use: http://stackoverflow.com/questions/8919/looking-for-best-practice-for-doing-a-net-use-in-c-sharp – Kiquenet May 13 '13 at 06:34

3 Answers3

11

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");

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;
raveturned
  • 2,637
  • 24
  • 30
  • Maybe Better practices for net use: http://stackoverflow.com/questions/8919/looking-for-best-practice-for-doing-a-net-use-in-c-sharp – Kiquenet May 13 '13 at 06:35
2

You need specify the path to net.exe which is located in a system directory (i.e. windows\system32). For example,

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

Also note that command line arguments are passed as a second parameter.

VinayC
  • 47,395
  • 5
  • 59
  • 72
  • 1
    While it is better to specify the path to the command you want use, it is not strictly necessary as long as the command is on the user's path. – raveturned Mar 28 '12 at 10:50
1
 System.Diagnostics.ProcessStartInfo proccessStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "net user " + id + "/domain");
ionden
  • 12,536
  • 1
  • 45
  • 37