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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9600453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:03:43  来源:igfitidea点击:

Command line arguments in C# application

c#wpf

提问by Cipher

I have a WPF C# application, to which I have to pass command line argument. The argument is actually a URL, which I have to then use in my application?

我有一个 WPF C# 应用程序,我必须向它传递命令行参数。参数实际上是一个 URL,然后我必须在我的应用程序中使用它?

How are these command line arguments passed in WPF C#, so that the application can pickup the url during launch?

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

采纳答案by linquize

In your App.xaml.cs

在你的 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
    }
}

回答by AngeloBad

You can pass arguments like "no-wpf" C# applications through comman line. The difference is the application entry point. In WPF is App.xaml.cs. So, you have in this file you can pick arguments in this way:

您可以通过命令行传递诸如“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
    }
}

回答by Andreas Kahler

It has been mentioned by linquize above, but I think it is worth an answer of its own, as it is so simple...

上面的 linquize 已经提到过,但我认为值得自己回答,因为它是如此简单......

You can just use:

你可以只使用:

string[] args = Environment.GetCommandLineArgs();

That works anywhere in the application, not just in App.xaml.cs

这适用于应用程序的任何地方,而不仅仅是在 App.xaml.cs 中