这是我的代码。输入的命令行是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
信息(我找不到)。
但我也想知道是否有更干净的方法来完成这一切。
As for 我想要实现这个目标的原因,这是解释(tl;dr:从命令行解析 json):https://stackoverflow.com/a/36203572/831138 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 示例一起使用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)