Search code examples
c#visual-studio-2010debuggingvisual-studio-debugging

Is it possible to change command line arguments while debugging?


I have created a method which gives different message box output results depending on the passed command line arguments.

Currently I have to start debugging every time I want to change the command line arguments string.

Is there a way to change the command line arguments during a debugging session?

EDIT: I've added some sample code

private static class MyParsers
    {

    public static List<string> args;

    static MyParsers()
    {
        args = Environment.GetCommandLineArgs().ToList();
    }

        public static List<string> ParseOptions()
        {
            return ParseOptions(true);
        }

        public static List<string> ParseOptions(bool caseSensitive)
        {
            return caseSensitive
                   ? args
                   : args.MyExtToLower();
        }

        public static bool OptionExists(string option)
        {
            return OptionExists(option, true);
        }

        public static bool OptionExists(string option, bool caseSensitive)
        {
            return caseSensitive
                       ? ParseOptions().Contains(option)
                       : ParseOptions().MyExtToLower().Contains(option);
        }

        public static bool OptionExists(string option, string delimiter)
        {
            return OptionExists(option, false, delimiter);
        }

        public static bool OptionExists(string option, bool caseSensitive, string delimiter)
        {
            var args = ParseOptions(caseSensitive);
            for (var i = 1; i < args.Count; i++)
            {
                if (args[i].Contains(option + delimiter)) return true;
            }
            return false;
        }
}

Then I call MessageBox.Show(MyParsers.OptionExists("/list","=").ToString());

If the command line argument is /list=blah it returns true.

If the command line argument is /listary it returns false.

What method would you suggest for quickly altering the command line parameters? considering the above code I am using.


Solution

  • The commandline that a process was started with cannot be changed. However, you could copy your arguments into an easily-accessible "settings" object early in your application, and then manipulate that instead.

    EDIT: Instead of the properties in your object parsing the command line every time you call them, change the properties to regular properties. Then have an Initialise method (or use the constructor, even) so that you parse the command line just once at startup. Then, you can use the immediate window to change the values of your properties at will, because they're not referring back to the command line every time.