I have a tool which I want to run from my command prompt.
The code is as
static void Main(string[] args)
{
string User;
if (args[0].Length != 0)
{
User = args[0];
}
else
{
Console.Write("Please Enter the Username");
User = Console.ReadLine();
}
If I didnt give the username or the first argument after my ‘tool.exe’ in command prompt, it throws an exception like “Index was outside the bounds of the array”
I want ouptut as, if I didnt give argument – it should prompt me to give the username. please help me out.
argsis an array, and is what you should be checking for length. When you checkargs[0].Lengthyou’re actually assuming there’s atleast one element in the array already and thus you’re checkingLengthof the first item.Try
instead, which checks the length of the array of command line parameters.