这是我的代码。输入命令行为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
而不是使用
var rawCmd = Environment.CommandLine;
您可以使用:
var rawCmd = Environment.CommandLine;
var argsOnly = rawCmd.Replace("\"" + Environment.GetCommandLineArgs()[0] + "\"", "");
在您的示例中,这将返回“ var1 val1 var2 val2”。它应该与另一篇文章中的JSON示例一起使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句