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();
            }
        }
    }
3
  • do you mean "how do I read the arguments passed to my program?" Commented Jul 18, 2012 at 13:50
  • 1
    possible duplicate of Run Command Prompt Commands Commented Jul 18, 2012 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 :) Commented Jul 18, 2012 at 14:06

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

his project is a console app. so he doesn't need a new cmd window.
0

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

 String[] arguments = Environment.GetCommandLineArgs();

Comments

0

Just have a look at the

string[] args

Your command-line arguments are inside the string array.

Comments

0

the arguments sent to your program are stored in args

static void Main(string[] args) 

Comments

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

Comments

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

2 Comments

Can I pass multiple args[] inside switch ?
absolutely, just separate each arg with a new case statement. See my update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.