【发布时间】:2016-07-12 07:05:59
【问题描述】:
这是我的代码。输入命令行为var1 val1 var2 val2:
var rawCmd = Environment.CommandLine;
// Environment.CommandLine adds the .exe info that I don't want in my command line:
// rawCmd = "path\to\ProjectName.vshost.exe" var1 val1 var2 val2
// A. This correction makes it work, although it is pretty ugly:
var cleanCmd = rawCmd.Split(new string[] { ".exe\" " }, StringSplitOptions.None)[1];
// B. This alternative should be cleaner, but I can't make it work:
var exePath = System.Reflection.Assembly.GetCallingAssembly().Location;
cleanCmd = rawCmd.Replace(string.Format($"\"{exePath}\" "), "");
所以为了使 B 工作,我应该能够找到.vhost.exe 信息(我无法找到)。
但我也想知道是否有更清洁的方法来完成这一切。
至于我要实现这个的原因,解释如下(tl;dr:从命令行解析一个json):https://stackoverflow.com/a/36203572/831138
【问题讨论】:
-
为什么不能将传递给
Main方法的args参数存放在方便的位置? -
你为什么将命令行作为单个
string处理?据我了解,参数已经作为数组传递,如here 所述。 -
原因是我在帖子底部链接的示例:stackoverflow.com/a/36203572/831138。目标是从命令行解析一个 json。
-
您不能像
myExe < file.json或echo JSON | myExe那样通过管道传输JSON 吗? -
@AlexeiLevenkov 我从 VS2015 插入命令行以调试我的代码。然后即使代码在生产中,考虑到我的设置,只要能够摆脱 cmd 的 exe 部分就会更容易。
标签: c#