-1

I am currently trying to debug an application and I am passing debug command line arguments via Visual Studio when starting the application as this is the only way available to me to debug this.

When I use either the > or < symbols in the command line arguments they are simply ignored. This does not happen when the application is called via command prompt however.

I have tried using 'ampersand' gt; , but this did not work. Can anyone please advise?

EDIT: The code I'm using is

Processor.CommandLineArgs = My.Application.CommandLineArgs

An example of it being used is me passing "/output.txt />3"

My.Application.CommandLineArgs has 2 items, "/output.txt" and "/3"

Rufus L
  • 36,127
  • 5
  • 30
  • 43
Matt
  • 151
  • 1
  • 10
  • 3
    Can you **[edit]** the question and give actual examples of what commands or parameters you are tring to pass, and also explain what you are trying to achieve by doing so? – Peter B Feb 25 '19 at 15:22
  • Can you post the code from your main method because it works perfectly. I just tried putting this `< = asd` into the command line arguments and i receive an array of 3 which are `<`, `=` and `asd` – Franck Feb 25 '19 at 15:24
  • @Franck The code I'm using is
    Processor.CommandLineArgs = My.Application.CommandLineArgs
    An example of it being used is me passing "/output.txt />3"
    My.Application.CommandLineArgs has 2 items, "/output.txt" and "/3"
    – Matt Feb 25 '19 at 15:29
  • @Matt format it in your question. not in comment – Franck Feb 25 '19 at 15:35
  • Welp. Screwed up that formatting. – Matt Feb 25 '19 at 15:35
  • edited that for you @Franck – Matt Feb 25 '19 at 15:37
  • `>` or `<` aren't valid in arguments from a bare command line; they're shell redirection operators. If you want them in arguments, quote them (`"/output.txt \"/>3\""`). If you actually want to use `>` or `<` to redirect input or output, you cannot do this using command line arguments; you have to redirect standard input/output as appropriate. On the command prompt, something like `myapp /output.txt />3` will actually send all output of `myapp /output.txt /` to the file named `3`. – Jeroen Mostert Feb 25 '19 at 16:12
  • Thanks Jeroen, this was the answer. Much appreciated. – Matt Feb 25 '19 at 16:18
  • Also see this answer, not sure if it's applicable: https://stackoverflow.com/questions/251557/escape-angle-brackets-in-a-windows-command-prompt – Rufus L Feb 25 '19 at 16:44

1 Answers1

0

If your application is a console application simply store the values you receive directly from the Program.cs Main(string[] args) method. It will contain all you need.

If you are using a winform project you can simply edit the same file and also read the parameters from there instead. Here a simple example of a modified program.cs of a brand new winform project

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args) // just added the args here
    {
        var myParams = args; // read the values and do something. This conserve special characters

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}
Franck
  • 4,438
  • 1
  • 28
  • 55
  • While this would work, it's not something I can implement. The code is being used on an enterprise scale and I can't alter the structure of the main function (which currently isn't passed an array of args). – Matt Feb 25 '19 at 16:19
  • @Matt Yes you can implement, it's a couple characters to add the extra functionality of reading params being pass at the entry point. You can do a simple if statement and check if `args` length is zero then it's called as usual. Else it mean parameter were passed. – Franck Feb 25 '19 at 16:25
  • I meant I can't implement because this is code I'm not allowed to change. This is not personal code, but from my employer. I am not able to alter it. – Matt Feb 25 '19 at 16:31
  • @Matt That is the only way to catch the real parameters passed to any project. The only other solution as mentioned by Jeroen is to stop using these output characters as there is no other possible way to catch them. – Franck Feb 25 '19 at 16:34