128

I want to debug a program in Visual Studio 2008. The problem is that it exits if it doesn't get arguments. This is from the main method:

if (args == null || args.Length != 2 || args[0].ToUpper().Trim() != "RM") 
{
    Console.WriteLine("RM must be executed by the RSM.");
    Console.WriteLine("Press any key to exit program...");
    Console.Read();
    Environment.Exit(-1);
}

I don't want to comment it out and and then back in when compiling. How can I start the program with arguments when debugging? It is set as the StartUp Project.

senshin
  • 10,022
  • 7
  • 46
  • 59
Kasper Hansen
  • 6,307
  • 21
  • 70
  • 106
  • 1
    possible duplicate of [Passing command line parameters with Visual Studio C#](http://stackoverflow.com/questions/6475887/passing-command-line-parameters-with-visual-studio-c-sharp) – horns May 04 '15 at 13:49
  • 3
    Possible duplicate of [Debugging with command-line parameters in Visual Studio](http://stackoverflow.com/questions/298708/debugging-with-command-line-parameters-in-visual-studio) –  Oct 15 '15 at 09:24

6 Answers6

206

Go to Project-><Projectname> Properties. Then, click on the Debug tab. Then:

If you're using Visual Studio 2022

Continue by clicking Open debug launch profiles UI. Fill in your arguments in the textbox labeled Command line arguments.

If you're using a version before Visual Studio 2022

The Command Line Arguments textbox is available on the Debug tab. Fill your arguments in the textbox.

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
  • 1
    The arguments can (must?) be filled into the `Command line arguments` space separated (like you would do, using the command line). I'm not sure if there are other ways, but maybe you can add this to your answer. – d4Rk Apr 01 '15 at 17:08
  • 2
    I have been looking for this answer for a week! Thank you! – bird2920 Jan 05 '17 at 21:30
  • Don't forget speech marks around command arguments containing spaces, that caught me out. – Dale Jan 22 '18 at 14:53
  • Is it possible to use named parameters for when using libraries like command line parser? e.g. How can I pass something like `-url https://google.com -p pass -u user`? – Animesh Jan 29 '18 at 10:48
62

I would suggest using the directives like the following:

        static void Main(string[] args)
        {
#if DEBUG
            args = new[] { "A" };
#endif

            Console.WriteLine(args[0]);
        }

Good luck!

Homam
  • 23,263
  • 32
  • 111
  • 187
  • 2
    Both good answers, I like this more only because it is easier to change or reference (although it is not necessarily a hassle going through properties :p, maybe an extra mouse click or so) – Andrew Jackman Jan 25 '11 at 08:12
  • 4
    Omho this answer is THE answer. It is a tedious job to edit the project settings every time you want to debug with a new set of command line parameters. When it's written like this, you can just simply write down all the test cases you want to check and just toggle the comments on each to activate it. Much faster and proves especially useful if you're suddenly in front of a Visual Studio in a different language (not the one you are used to work with), which is exactly my case and although I know the language, the translation is awful and even a native speaker can't deal with it properly. :D – rbaleksandar Feb 14 '13 at 09:03
  • 1
    I agree with Homam's Solution. Though for a small program , setting the Project Properties -> Debug Tab's Command line arguments is a more direct and easy way to debug, for large applications using Directives are more helpful and elegant. – Sabitha Jan 25 '11 at 18:02
  • Both solutions are perfect. but, I kind of prefer the solution by Homam. Its elegant does not need to tinker with the project setings which one may forget to take care of. On a seconf thought one may also forget the code changes made but they are at least "visible". – IUnknown Aug 09 '11 at 10:17
5

My suggestion would be to use Unit Tests.

In your application do the following switches in Program.cs:

#if DEBUG
    public class Program
#else
    class Program
#endif

and the same for static Main(string[] args).

Or alternatively use Friend Assemblies by adding

[assembly: InternalsVisibleTo("TestAssembly")]

to your AssemblyInfo.cs.

Then create a unit test project and a test that looks a bit like so:

[TestClass]
public class TestApplication
{
    [TestMethod]
    public void TestMyArgument()
    {
        using (var sw = new StringWriter())
        {
            Console.SetOut(sw); // this makes any Console.Writes etc go to sw

            Program.Main(new[] { "argument" });

            var result = sw.ToString();

            Assert.AreEqual("expected", result);
        }
    }
}

This way you can, in an automated way, test multiple inputs of arguments without having to edit your code or change a menu setting every time you want to check something different.

gbdavid
  • 1,639
  • 18
  • 40
dav_i
  • 27,509
  • 17
  • 104
  • 136
5

I came to this page because I have sensitive information in my command line parameters, and didn't want them stored in the code repository. I was using System Environment variables to hold the values, which could be set on each build or development machine as needed for each purpose. Environment Variable Expansion works great in Shell Batch processes, but not Visual Studio.

Visual Studio Start Options:

Visual Studio Start Options

However, Visual Studio wouldn't return the variable value, but the name of the variable.

Example of Issue:

Example of Error in Visual Studio

My final solution after trying several here on S.O. was to write a quick lookup for the Environment variable in my Argument Processor. I added a check for % in the incoming variable value, and if it's found, lookup the Environment Variable and replace the value. This works in Visual Studio, and in my Build Environment.

foreach (string thisParameter in args)
            {
                if (thisParameter.Contains("="))
                {
                    string parameter = thisParameter.Substring(0, thisParameter.IndexOf("="));
                    string value = thisParameter.Substring(thisParameter.IndexOf("=") + 1);

                    if (value.Contains("%"))
                    {   //Workaround for VS not expanding variables in debug
                        value = Environment.GetEnvironmentVariable(value.Replace("%", ""));
                    }

This allows me to use the same syntax in my sample batch files, and in debugging with Visual Studio. No account information or URLs saved in GIT.

Example Use in Batch

Batch File Example

Billy Willoughby
  • 806
  • 11
  • 15
3

for .NET Core console apps you can do this 2 ways - from the launchsettings.json or the properties menu.

Launchsettings.json

enter image description here

or right click the project > properties > debug tab on left

see "Application Arguments:"

  • this is " " (space) delimited, no need for any commas. just start typing. each space " " will represent a new input parameter.
  • (whatever changes you make here will be reflected in the launchsettings.json file...)

enter image description here

Robert Green MBA
  • 1,834
  • 1
  • 22
  • 45
2

For Visual Studio Code:

  • Open launch.json file
  • Add args to your configuration:

"args": ["some argument", "another one"],

Alamakanambra
  • 5,845
  • 3
  • 36
  • 43