簡體   English   中英

使用C#將參數傳遞給命令提示符

[英]Pass arguments to a command prompt using c#

我是C#的新手,正嘗試在本地計算機上禁用或啟用用戶,如下面的代碼所示。 我正在創建一個exe,並提示用戶輸入要啟用或禁用的用戶名。

現在,我想將參數傳遞給命令提示符並禁用或啟用用戶。 例如:> cmd.exe John Disable。

如何使用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();

這是在控制台中使用ping命令的示例。 您可以添加其他選項,例如強制其不打開gui等。

您可以使用Environment.CommandLine讀取命令行參數或Environment.GetCommandLineArgs()方法。

 String[] arguments = Environment.GetCommandLineArgs();

看看

string[] args

您的命令行參數在字符串數組內。

發送到程序的參數存儲在args

static void Main(string[] args) 

采用:

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

  }

例如

 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
}

這使您可以說出prog.exe --test-net並運行該特定代碼。

- 編輯 -

在這種情況下,可以使用多個arg將命令串在一起

prog.exe --test-net --loop 

您可以根據需要擁有任意數量的參數。 如果您想為arg使用人工輸入,則始終可以控制args的數量並獲取args [x + 1]以獲取要禁用/啟用的人員的姓名。

這是假設您的case語句有2種情況:例如--enable和--disable。 然后,您可以像這樣調用程序:

prog.exe --enable John

這是你的主嗎? 如果是這樣,您可以從string [] args中引用命令行參數:

static void Main(string[] args)

您可以在此處看到一些示例: 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