简体   繁体   中英

Pass arguments to a command prompt using c#

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();
            }
        }
    }
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.

您可以使用Environment.CommandLine读取命令行参数或Environment.GetCommandLineArgs()方法。

 String[] arguments = Environment.GetCommandLineArgs();

Just have a look at the

string[] args

Your command-line arguments are inside the string array.

发送到程序的参数存储在args

static void Main(string[] args) 

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM