c# - 如何在 C# 中使用网络用户

标签 c# .net shell command

我正在尝试将 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异常,消息系统找不到指定的文件

异常详情如下

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 开始的所有内容都是命令参数。因此,您需要使用以下构造函数:

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

此外,为了捕获标准输出,您需要在调用 proc.Start()

之前设置以下属性
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;

关于c# - 如何在 C# 中使用网络用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9905719/

相关文章:

c# - 将防伪与 Ajax 或 AngularJs 结合使用

.net - 使用 LoadControl(Type, Object()) 以编程方式加载用户控件

xml - 使用 Shell 更新 XML 属性的值

c# - 在 Web 应用程序中传递用户信息的最佳方式是什么?

c# - 列表中的 LINQ 查询列表

c# - Unity Networking-UNet 中的附加场景加载

.net - MemoryStream 如何处理分配的缓冲区?

.net - Settings.setting 在 Debug 模式下给出不同的值

c - gdb 退出而不是生成 shell

linux - 如何使用命令行删除文件夹中特定文件类型的所有文件