C# WPF 命令行参数,一种聪明的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9343381/
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
WPF Command Line Arguments, a smart way?
提问by Sandeep Bansal
I'm looking for a way that I can parse command line arguments into my WPF application with just a way of reading the value of the argument that the user passed.
我正在寻找一种方法,只需一种读取用户传递的参数值的方法,就可以将命令行参数解析到我的 WPF 应用程序中。
As an example
举个例子
application.exe /setTime 5
is there a way for me to have some code where I can just say:
有没有办法让我有一些代码,我可以说:
MessageBox.Show(arg("setTime"));
Which will output 5
哪个会输出 5
Working Solution
工作解决方案
采纳答案by ChrisF
The way I always do it is to specify the arguments as a "name"/"value" pair e.g.
我总是这样做的方式是将参数指定为“名称”/“值”对,例如
myprogram.exe -arg1 value1 -arg2 value2
This means that when you parse the command line you can put the argument/value pairs in a Dictionarywith the argument as the key. Then your arg("SetTime")will become:
这意味着当您解析命令行时,您可以将参数/值对放在 a 中Dictionary,并将参数作为键。那么你的arg("SetTime")意志会变成:
MessageBox.Show(dictionary["SetTime"]);
(Obviously you don't want the actual dictionary to be public.)
(显然你不希望实际的字典是公开的。)
To get the arguments in the first place you can use:
要首先获取参数,您可以使用:
string[] args = Environment.GetCommandLineArgs();
This will return all the arguments so you will need to parse the array in steps of two (after first checking that the length is a multiple of two + 1):
这将返回所有参数,因此您需要分两步解析数组(首先检查长度是否为 2 + 1 的倍数):
The first element of the array is the name of the executing program - MSDN Page- so your loop needs to start from one:
数组的第一个元素是执行程序的名称 - MSDN Page- 所以你的循环需要从一个开始:
for (int index = 1; index < args.Length; index += 2)
{
dictionary.Add(args[index], args[index+1]);
}
This loops in steps of two as you define each argument is a pair of values: the identifier and the actual value itself, e.g.
当您定义每个参数是一对值时,这将分两步循环:标识符和实际值本身,例如
my.exe -arg1 value1 -arg2 value2
Then you can simply see if the argument is specified by seeing if the key -arg1is in the dictionary and then read it's value:
然后,您可以通过查看键-arg1是否在字典中来简单地查看是否指定了参数,然后读取它的值:
string value;
if (dictionary.TryGetValue(arg, out value))
{
// Do what ever with the value
}
This means you can have the arguments in any order and omit any arguments you don't want to specify.
这意味着您可以按任何顺序使用参数并省略您不想指定的任何参数。
The only drawback with this method is if you have a flag like -debug(for example) which could be logically implemented with the presence or absence of the flag will need to be specified as -debug true(or 1or on), but it does simplify things if you have flags that dorequire values (like configuration file paths, database connection strings etc.)
这种方法的唯一缺点是,如果你有一个像-debug(例如)这样的标志,它可以在逻辑上实现标志的存在或不存在需要指定为-debug true(或1或on),但如果你有标志,它确实简化了事情即不要求值(例如配置文件路径,数据库连接字符串等)
回答by Paul Karam
There's another way to do this in WPF. Here's an articleabout it, and here's the steps to take:
在 WPF 中还有另一种方法可以做到这一点。这是一篇关于它的文章,这里是要采取的步骤:
First, you open App.xamland you add Startup="Application_Startup"after the StartupUri="Window1.xaml", so your App.xamlwill look like this:
首先,您打开App.xaml并在Startup="Application_Startup"之后添加StartupUri="Window1.xaml",因此您App.xaml将如下所示:
<Application x:Class="ParametersForWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml"
Startup="Application_Startup">
<Application.Resources>
</Application.Resources>
</Application>
Then function Application_Startupwill automatically be added to your App.xaml.csfile:
然后函数Application_Startup将自动添加到您的App.xaml.cs文件中:
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
}
}
Now inside this function you can check the argssent to the application. An example to do this is:
现在在此函数中,您可以检查args发送到应用程序的内容。这样做的一个例子是:
private void Application_Startup(object sender, StartupEventArgs e)
{
foreach(string s in e.Args)
{
MessageBox.Show(s);
}
}
If you need them as a Dictionarythen you could easily implement ChrisF's answerinside the Application_Startupfunction.
如果您需要它们,Dictionary那么您可以轻松地在函数内实现ChrisF 的答案Application_Startup。

