c# - 读取命令行参数及其相关值
c# - Reading commandline args with their associated values
只是想知道是否有一种方法可以在带有值的控制台应用程序中读取命令行。我可以很容易地阅读命令行,但似乎找不到任何有关从命令行获取值的信息。
例如(args):
aTest = 5; bTest = 13;
是否有办法读取 arg aTest
并将其与 int 5...相关联?
谢谢:)
很久以前我已经为我写了一个辅助方法,它提取了 swtich 或 returns 的值(如果存在或不存在)- 这可能会对您有所帮助。
/// <summary>
/// If the arguments are in the format /member=value
/// Than this function returns the value by the given membername (!casesensitive) (pass membername without '/')
/// If the member is a switch without a value and the switch is preset the given ArgName will be returned, so if switch is presetargname means true..
/// </summary>
/// <param name="args">Console-Arg-Array</param>
/// <param name="ArgName">Case insensitive argname without /</param>
/// <returns></returns>
private static string getArgValue(string[] args, string ArgName)
{
var singleFound = args.Where(w => w.ToLower() == "/" + ArgName.ToLower()).FirstOrDefault();
if (singleFound != null)
return ArgName;
var arg = args.Where(w => w.ToLower().StartsWith("/" + ArgName.ToLower() + "=")).FirstOrDefault();
if (arg == null)
return null;
else
return arg.Split('=')[1];
}
示例:
static void Main(string[] args)
{
var modeSwitchValue = getArgValue(args, "mode");
if (modeSwitchValue == null)
{
//Argument not present
return;
}
else
{
//do something
}
}
根据提供的内容查找您的输入:
var args = "aTest = 5; bTest = 13;";
var values =
args
.Split(new [] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim().Split('=').Select(y => y.Trim()).ToArray())
.ToDictionary(x => x[0], x => int.Parse(x[1]));
我得到这个结果:
只是想知道是否有一种方法可以在带有值的控制台应用程序中读取命令行。我可以很容易地阅读命令行,但似乎找不到任何有关从命令行获取值的信息。
例如(args):
aTest = 5; bTest = 13;
是否有办法读取 arg aTest
并将其与 int 5...相关联?
谢谢:)
很久以前我已经为我写了一个辅助方法,它提取了 swtich 或 returns 的值(如果存在或不存在)- 这可能会对您有所帮助。
/// <summary>
/// If the arguments are in the format /member=value
/// Than this function returns the value by the given membername (!casesensitive) (pass membername without '/')
/// If the member is a switch without a value and the switch is preset the given ArgName will be returned, so if switch is presetargname means true..
/// </summary>
/// <param name="args">Console-Arg-Array</param>
/// <param name="ArgName">Case insensitive argname without /</param>
/// <returns></returns>
private static string getArgValue(string[] args, string ArgName)
{
var singleFound = args.Where(w => w.ToLower() == "/" + ArgName.ToLower()).FirstOrDefault();
if (singleFound != null)
return ArgName;
var arg = args.Where(w => w.ToLower().StartsWith("/" + ArgName.ToLower() + "=")).FirstOrDefault();
if (arg == null)
return null;
else
return arg.Split('=')[1];
}
示例:
static void Main(string[] args)
{
var modeSwitchValue = getArgValue(args, "mode");
if (modeSwitchValue == null)
{
//Argument not present
return;
}
else
{
//do something
}
}
根据提供的内容查找您的输入:
var args = "aTest = 5; bTest = 13;";
var values =
args
.Split(new [] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim().Split('=').Select(y => y.Trim()).ToArray())
.ToDictionary(x => x[0], x => int.Parse(x[1]));
我得到这个结果: