C#应用程序中的命令行参数

时间:2012-03-07 11:17:48

标签: c# wpf

我有一个WPF C#应用程序,我必须通过命令行参数。该参数实际上是一个URL,我必须在我的应用程序中使用它?

如何在WPF C#中传递这些命令行参数,以便应用程序可以在启动期间获取URL?

3 个答案:

答案 0 :(得分:56)

在App.xaml.cs

class App : Application
{
    //Add this method override
    protected override void OnStartup(StartupEventArgs e)
    {
        //e.Args is the string[] of command line argruments
    }
}

答案 1 :(得分:28)

上面的linquize已经提到了,但我认为值得回答一下,因为它很简单...

你可以使用:

string[] args = Environment.GetCommandLineArgs();

它适用于应用程序中的任何位置,而不仅仅是在App.xaml.cs

答案 2 :(得分:0)

你可以通过comman行传递像“no-wpf”C#应用程序这样的参数。 不同之处在于应用程序入口点。在WPF中是App.xaml.cs.因此,您可以在此文件中以这种方式选择参数:

class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        //e.Args represent string[] of no-wpf C# applications
    }
}