To set command-line arguments in Visual Studio, right click on the project name, then go to Properties. In the Properties Pane, go to "Debugging", and in this pane is a line for "Command-line arguments." Add the values you would like to use on this line. They will be passed to the program via the argv array.
Go to Project-><Projectname> Properties
. Then click on the Debug
tab, and fill in your arguments in the textbox called Command line arguments
.
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!
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.
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:
However, Visual Studio wouldn't return the variable value, but the name of the variable.
Example of Issue:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With