環境類提供有關當前平台的信息並操作當前平台。它對於獲取和設置各種操作 system-related 信息很有用。我們可以使用它來檢索 命令行 參數信息、退出代碼信息、環境變量設置信息、調用堆棧信息的內容以及自上次係統啟動以來的時間(以毫秒為單位)信息。在本文中,我們將演示環境類的 GetCommandLineArgs() 方法的使用。此方法用於獲取當前進程的 命令行 參數,並返回一個字符串數組,其中包含在命令行上傳遞的參數。 命令行 參數通常由空格分隔,我們可以使用雙引號 (") 在參數中包含空格。而單引號 (‘) 不提供相同的函數。當雙引號後跟偶數個反斜杠時,則開始的一對反斜杠被替換為一個反斜杠,並且雙引號也被刪除。例如:輸入:MyData \\\\”app1 \\\\”app2,輸出:MyData \\\\app1,\\app2。當雙引號後跟奇數個反斜杠時,則開始的一對反斜杠被一個反斜杠替換,其餘的反斜杠被刪除。在這種情況下,不會刪除雙引號。例如:輸入:MyData \\\\\”app1 \”app2,輸出:MyData \\”app1,“app2
用法:
public static string[] GetCommandLineArgs ()
返回:它返回一個字符串數組,其中包含當前正在運行的進程的 命令行 參數。在這個數組中,第一項是可執行文件名,其餘項是命令行參數。
異常:
NotSupportedException:當調用的方法不受支持,或者嘗試讀取、查找或寫入不支持調用的函數的流時,將引發此異常。
示例:
Input :c/cpp java python Output: c/cpp java python Input :11 java python Output: 11 java python
例:
C#
// C# program to illustrate the use of
// GetCommandLineArgs() Method of Environment Class
using System;
class GFG{
static public void Main()
{
// Declare an array of string that holds the
// command-line arguments for the running process
// Using GetCommandLineArgs() method of
// Environment class
string[] commandline = Environment.GetCommandLineArgs();
Console.WriteLine("Command Line Arguments are:");
// Display the command-line arguments for
// the current process
foreach(string arguments in commandline)
{
Console.WriteLine("\t" + arguments);
}
}
}
輸出:
Command Line Arguments are: /Users/Projects/newprogram/newprogram/bin/Debug/netcoreapp3.0/newprogram.dll
相關用法
注:本文由純淨天空篩選整理自manojkumarreddymallidi大神的英文原創作品 C# Program to Demonstrate the Use of GetCommandLineArgs() Method of Environment Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。