How to remove the exe part of the command line

c#

Solution

Instead of using

var rawCmd = Environment.CommandLine;

You can use:

var rawCmd = Environment.CommandLine;
var argsOnly = rawCmd.Replace("\"" + Environment.GetCommandLineArgs()[0] + "\"", "");

This will return "var1 val1 var2 val2" in your example. And it should work with the JSON example in the other post.

Problem

This is my code. The input command line is `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}\" "), ""); ``` So to make B work, I should be able to find the `.vhost.exe` info (which I am not able to find). But also I would like to know if there is a cleaner way to do all this. As for the reason why I want to achieve this, here is the explanation (tl;dr: parsing a json from the command line): https://stackoverflow.com/a/36203572/831138

Original source

Related problems