简体   繁体   English

使用C#将参数传递给命令提示符

[英]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. 我是C#的新手,正尝试在本地计算机上禁用或启用用户,如下面的代码所示。 I am creating a exe and prompting users to enter username which they want to enable or disable. 我正在创建一个exe,并提示用户输入要启用或禁用的用户名。

Now I want to pass the arguments to a command prompt and disable or enable users. 现在,我想将参数传递给命令提示符并禁用或启用用户。 For eg:>cmd.exe John Disable. 例如:> 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? 如何使用c#将参数传递给命令提示符并使用下面的相同代码来启用或禁用用户?

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. 这是在控制台中使用ping命令的示例。 You can add other options like forcing it to not open the gui etc. 您可以添加其他选项,例如强制其不打开gui等。

您可以使用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. 这使您可以说出prog.exe --test-net并运行该特定代码。

--edit-- - 编辑 -

With multiple args you can string together a command, in this instance 在这种情况下,可以使用多个arg将命令串在一起

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. 如果您想为arg使用人工输入,则始终可以控制args的数量并获取args [x + 1]以获取要禁用/启用的人员的姓名。

This is making an assumption that your case statements have 2 cases: --enable and --disable for instance. 这是假设您的case语句有2种情况:例如--enable和--disable。 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: 如果是这样,您可以从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 您可以在此处看到一些示例: http : //msdn.microsoft.com/zh-cn/library/aa288457(v=vs.71).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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