Process msgProcess = new Process();
            msgProcess.StartInfo.FileName = @"net.exe";
            msgProcess.StartInfo.CreateNoWindow = true;
            msgProcess.StartInfo.Arguments = "user wangxian11 wangxian /add ";
            msgProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            msgProcess.StartInfo.UseShellExecute = false;
            msgProcess.StartInfo.RedirectStandardOutput = true;
            msgProcess.Start();
            msgProcess.WaitForExit();

解决方案 »

  1.   

    msgProcess.StartInfo.Arguments = " user wangxian11 wangxian /add ";
    user 前应该有个空格
      

  2.   

    楼上说的是 !~中间有空格
    msgProcess.StartInfo.FileName = @"net.exe";
     msgProcess.StartInfo.FileName ="net";//这里应该只要就可以了
      

  3.   

    using System.DirectoryServices;
    see:
    //Creating Users
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sds/sds/creating_users.asp//Adding Users to a Group
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sds/sds/adding_users_to_a_group.asp
      

  4.   

    对,应该是调用的是net.exe,而不是调用cmd.exe
      

  5.   


    楼主,
    你这代码可以呀,
    偶试过啦可以建用户,你不你再 加上 @ 看看,如下:p.StandardInput.WriteLine(@"net user test2 test2 /add");// 向cmd.exe输入command net user wan
      

  6.   

    How to add a new user using DirectoryServices?
    //http://www.codeproject.com/dotnet/addnewuser.aspprivate void AddUser(string strDoamin, string strLogin, string strPwd)
    {
    DirectoryEntry obDirEntry = null;
    try
    {
    obDirEntry = new DirectoryEntry("WinNT://" + strDoamin);
    DirectoryEntries entries = obDirEntry.Children;
    DirectoryEntry obUser = entries.Add(strLogin, "User");
    obUser.Properties["FullName"].Add("Amigo");
    object obRet = obUser.Invoke("SetPassword", strPwd);
    obUser.CommitChanges();
    }
    catch (Exception ex)
    {
    Trace.Warn(ex.Message);
    }
    }
      

  7.   

    你的代码如果在B/S运行则没有足够的权限,修改web.config:
    <system.web>
    <identity impersonate="true" userName="administrator" password="xxxx"></identity>
    ...
    </system.web>
      

  8.   

    权限足够的话,除了使用Net.exe命令,也可以使用运行时身份验证:
    可将 GenericIdentity 类和 GenericPrincipal 类合起来使用,以创建独立于 Windows NT 或 Windows 2000 域的身份验证方案。
    执行下列任务来创建 GenericPrincipal 类的一个实例。 
    创建标识类的一个新实例,并用希望它持有的名称对其进行初始化。以下代码创建一个新的 GenericIdentity 对象,并用名称 MyUser 对其进行初始化。 
    [C#]
    GenericIdentity MyIdentity = new GenericIdentity("MyUser");
    [Visual Basic]
    Dim MyIdentity As New GenericIdentity("MyUser")
    接下来,创建 GenericPrincipal 类的一个新实例,并用先前创建的 GenericIdentity 对象和一组表示希望与此主体关联的角色的字符串对其进行初始化。以下代码示例指定表示一个管理员角色和一个用户角色的字符串数组。然后用前面的 GenericIdentity 和该字符串数组对 GenericPrincipal 进行初始化。 
    [C#]
    String[] MyStringArray = {"Manager", "Teller"};
    GenericPrincipal MyPrincipal = new GenericPrincipal(MyIdentity, MyStringArray);
    [Visual Basic]
    Dim MyStringArray As String() = {"Manager", "Teller"}
    DIm MyPrincipal As New GenericPrincipal(MyIdentity, MyStringArray)
    最后,使用以下代码将主体附加到当前线程中。这在以下几种情形中很有用:必须对主体进行多次验证,必须通过应用程序中运行的其他代码对主体进行验证,或必须由 PrincipalPermission 对象进行验证。不将主体附加到线程中,仍可对主体对象执行基于角色的验证。有关更多信息,请参见替换主体对象。 
    [C#]
    Thread.CurrentPrincipal = MyPrincipal;
    [Visual Basic]
    Thread.CurrentPrincipal = MyPrincipal
    以下代码实例说明如何创建 GenericPrincipal 和 GenericIdentity 的实例。此代码将这些类的值显示到控制台。
    [C#]
       using System;
       using System.Security.Principal;
       using System.Threading;    public class Class1
        {
            public static int Main(string[] args)
            {
             //Create generic identity.
             GenericIdentity MyIdentity = new GenericIdentity("MyIdentity");         //Create generic principal.
             String[] MyStringArray = {"Manager", "Teller"};
             GenericPrincipal MyPrincipal = new GenericPrincipal(MyIdentity, MyStringArray);
             
             //Attach the principal to the current thread.
             //This is not required unless repeated validation must occur,
             //other code in your application must validate, or the 
             // PrincipalPermisson object is used. 
             Thread.CurrentPrincipal = MyPrincipal;         //Print values to the console.
             String Name =  MyPrincipal.Identity.Name;
             bool Auth =  MyPrincipal.Identity.IsAuthenticated; 
             bool IsInRole =  MyPrincipal.IsInRole("Manager");
             
             Console.WriteLine("The Name is: {0}", Name);
             Console.WriteLine("The IsAuthenticated is: {0}", Auth);
             Console.WriteLine("Is this a Manager? {0}", IsInRole);
             
             return 0;
            }
        }
    [Visual Basic]
    Imports System
    Imports System.Security.Principal
    Imports System.ThreadingPublic Class Class1
        
        Public Shared Sub Main()
            'Create generic identity.
            Dim MyIdentity As New GenericIdentity("MyIdentity")
            
            'Create generic principal.
            Dim MyStringArray As String() =  {"Manager", "Teller"}
            Dim MyPrincipal As New GenericPrincipal(MyIdentity, MyStringArray)
            
            'Attach the principal to the current thread.
            'This is not required unless repeated validation must occur,
            'other code in your application must validate, or the 
            ' PrincipalPermisson object is used. 
            Thread.CurrentPrincipal = MyPrincipal
            
            'Print values to the console.
            Dim Name As String = MyPrincipal.Identity.Name
            Dim Auth As Boolean = MyPrincipal.Identity.IsAuthenticated
            Dim IsInRole As Boolean = MyPrincipal.IsInRole("Manager")
            
            Console.WriteLine("The Name is: {0}", Name)
            Console.WriteLine("The IsAuthenticated is: {0}", Auth)
            Console.WriteLine("Is this a Manager? {0}", IsInRole)
            
        End Sub
        
    End Class
    执行时,应用程序显示类似于下面这样的输出。
    The Name is: MyIdentity
    The IsAuthenticated is: True
    Is this a Manager? True