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