How to error check for more than a certain amount of Command Parameters
I'm not quite sure how to google search this or put it into one sentence but here is my scenario.
I am creating a simple program in C# that one feature of it is to take command parameters and to get a directory from a certain command parameter and an output in another command parameter. I have 2 parameters that the first one is InputPath and the 2nd is Output path. Pretty basic.
I'm doing error checking to see if the directorys they placed are valid using
if(Directory.开发者_如何转开发Exists(args[0])&Directory.Exists(args[1]))
{
GenManifest(args[0], args[1]);
}
My question is how can I make it so if they place more than 2 command parameters that I can place an error like follows
MessageBox.Show("Please only insert 2 arguements","Error");
I also have a simple
else
{
MessageBox.Show("Invalid arguement format","Error");
}
to cover the majority of all other errors.
I'm also thinking of other ways to error check my code but for now i want the directories to be valid and to have the proper amount of arguements.
Thank you!
Daniel Sterba
Just check the length of the args array:
if (args.Length != 2)
{
// Display error
}
if (args.Length != 2)
{
MessageBox.Show("Please only insert 2 arguements","Error");
}
Also you should change &
to &&
so:
if (Directory.Exists(args[0]) && Directory.Exists(args[1]))
{
GenManifest(args[0], args[1]);
}
Here if the first condition returns false the second condition will be ignored.
精彩评论