0

I am new to C# and I am trying disable or enable users at local computer as shown in the code below. I am creating a exe and prompting users to enter username which they want to enable or disable.

Now I want to pass the arguments to a command prompt and disable or enable users. For eg:>cmd.exe John Disable.

How to pass arguments to a command prompt using c# and use the same code below to enable or disable users?

class EnableDisableUsers
    {
        static void Main(string[] args)
        {
                Console.WriteLine("Enter user account to be enabled or disabled");
                string user = Console.ReadLine();
                Console.WriteLine("Enter E to enable or D to disable the user account");
                string enableStr = Console.ReadLine();
                bool enable;

            if (enableStr.Equals("E") || enableStr.Equals("e"))
            {


                PrincipalContext ctx = new PrincipalContext(ContextType.Machine);

                // find a user
                UserPrincipal username = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, user);

                if (user != null)
                {
                    try
                    {
                        //Enable User
                        username.Enabled = true;
                        username.Save();
                        Console.WriteLine(user + " Enabled");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Operation failed - Username is not valid", e.Message);
                    }
                }
                Console.ReadLine();
            }
            else if (enableStr.Equals("D") || enableStr.Equals("d"))
            {


                PrincipalContext ctx = new PrincipalContext(ContextType.Machine);

                // find a user
                UserPrincipal username = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, user);

                if (user != null)
                {
                    try
                    {
                        //Disable User
                        username.Enabled = false;
                        username.Save();
                        Console.WriteLine(user + " Disabled");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Operation failed - Username is not valid", e.Message);
                    }
                }
                Console.ReadLine();
            }
        }
    }
Forte L.
  • 2,772
  • 16
  • 25
user1528803
  • 67
  • 3
  • 8
  • do you mean "how do I *read* the arguments passed to my program?" – hometoast Jul 18 '12 at 13:50
  • 1
    possible duplicate of [Run Command Prompt Commands](http://stackoverflow.com/questions/1469764/run-command-prompt-commands) – sloth Jul 18 '12 at 13:52
  • If any one of the 6 answers provided have resolved your issue, please mark the answer as correct with the check mark. Use the up arrows to show that a post is helpful, even if it does not solve your issue. Doing so will ensure continued help in the future for any questions you may have :) – Robert H Jul 18 '12 at 14:06

6 Answers6

1
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.Arguments = "/c ping " + machine;
processStartInfo.FileName = "cmd.exe";
Process process = new Process();
process.StartInfo = processStartInfo;
process.Start();

Here's an example using the ping command in console. You can add other options like forcing it to not open the gui etc.

ecMode
  • 530
  • 3
  • 16
0

You may use Environment.CommandLine to read command line argument or Environment.GetCommandLineArgs() methods.

 String[] arguments = Environment.GetCommandLineArgs();
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

Just have a look at the

string[] args

Your command-line arguments are inside the string array.

kimar
  • 191
  • 2
  • 8
0

the arguments sent to your program are stored in args

static void Main(string[] args) 
hometoast
  • 11,522
  • 5
  • 41
  • 58
0

Use:

 switch (args[x])
 {
      .... 

  }

for example

 switch (args[x])
 {
  #region --loop
  case "--loop":               
      x = -1;
      break;
 #endregion

 #region --test-net
     case "--test-net":
          Task isAlive = Task.Factory.StartNew(() =>
          {
              bool alive = tlib.CheckForInternetConnection();
              if (!alive)
              {
                  while (!alive)
                  {
                      Console.WriteLine("No connectivity found.");
                      System.Threading.Thread.Sleep(9000);
                      alive = tlib.CheckForInternetConnection();
                  }
              }
              else
              {
                   //TODO: Add alive code here 
              }
          });
          isAlive.Wait();
          break;

          #endregion
}

This allows you to say prog.exe --test-net and run that specific code.

--edit--

With multiple args you can string together a command, in this instance

prog.exe --test-net --loop 

You can have as many args as you want. If you want to use human input for an arg you can always control the amount of args and grab args[x+1] to get the name of the person to disable/enable.

This is making an assumption that your case statements have 2 cases: --enable and --disable for instance. Then you can call the program like:

prog.exe --enable John
Robert H
  • 11,520
  • 18
  • 68
  • 110
0

Is this in your Main? If so, you would refer to the command line arguments from the string[] args:

static void Main(string[] args)

You can see some examples here: http://msdn.microsoft.com/en-us/library/aa288457(v=vs.71).aspx

Andrew Backes
  • 1,884
  • 4
  • 21
  • 37