Environment..::.GetCommandLineArgs 方法 :
返回包含當前進程的命令列參數的字串數組。
文法:
public static string[] GetCommandLineArgs()
傳回值
類型:System.String[]
字串數組,其中的每個元素都包含一個命令列參數。第一個元素是可執行檔名,後面的零個或多個元素包含其餘的命令列參數。
備忘:
數組中的第一個元素包含執行程式的檔案名稱。如果該檔案名稱不可用,則第一個元素等於 String..::.Empty。其他元素包含在命令列輸入的任何附加標記。
程式檔案名稱可以(但不是必須)包含路徑資訊。
命令列參數由空格分隔。可以使用雙引號 (") 在參數中包含空格。但是,單引號 (') 不提供此功能。
如果兩個或偶數個反斜線後跟雙引號,則前面的每個反斜線對被一個反斜線替代,並且雙引號被刪除。如果奇數個(包括僅僅一個)反斜線後跟雙引號,則前面的每個反斜線對被一個反斜線替代,其餘的反斜線被刪除;但在此情況下,雙引號不會被刪除。
下表顯示如何分隔命令列參數,並假定 MyApp 為當前執行的應用程式。
命令列上的輸入內容 |
產生的命令列參數 |
MyApp alpha beta |
MyApp, alpha, beta |
MyApp "alpha with spaces" "beta with spaces" |
MyApp, alpha with spaces, beta with spaces |
MyApp 'alpha with spaces' beta |
MyApp, 'alpha, with, spaces', beta |
MyApp \\\alpha \\\\"beta |
MyApp, \\\alpha, \\bet |
MyApp \\\\\"alpha \"beta |
MyApp, \\"alpha, "beta |
若要擷取作為單個字串的命令列,請使用 CommandLine 屬性。
Example代碼:
using System;
……
using System.Windows.Forms;
namespace ReadDocument
{
public partial class FrmReadDocument : Form
{
public FrmReadDocument()
{
InitializeComponent();
}
private void FrmReadDocument_Load(object sender, EventArgs e)
{
this.Location = new Point(0,0);
//string[] cmdArgs = Environment.GetCommandLineArgs();
//string strCmdArgs = "";
//if (cmdArgs.Count() > 0)
//{
// foreach (string str in cmdArgs)
// {
// strCmdArgs = strCmdArgs + str + "\n";
// }
//}
//MessageBox.Show(strCmdArgs);
//Process process = Process.Start(str, "\"" + FileName + "\"");
try
{
string[] arg = Environment.GetCommandLineArgs();
string argstrs = String.Join(",", arg);
//MessageBox.Show("argstrs: "+argstrs);
string[] argPath = argstrs.Split(",".ToCharArray());
string filePath = argPath[1].ToString();
string strarr = "*.exe: " + argPath[0].ToString() + "\r\n" + "args: " + argPath[1].ToString();
//MessageBox.Show(strarr);
this.webBrowser1.Navigate(filePath, false);
this.Activate();
}
catch (Exception ex)
{
Logs.WriteLog(ex.Message.ToString());
}
}
private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.Close();
this.Dispose();
}
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.webBrowser1.Visible = true;
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
this.BackgroundImage = ReadDocument.Properties.Resources.load;
webBrowser1.Visible = false;
}
private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
e.Cancel = true;
this.webBrowser1.Navigate(((System.Windows.Forms.WebBrowser)sender).StatusText, false);
}
private void FrmReadDocument_FormClosed(object sender, FormClosedEventArgs e)
{
this.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}