passing an empty string as Directory parameter

1.6k views Asked by At

I was wandering if you can help with this. I have a console app which takes a string Directory as input.

I want to put in a check in place which allows me to check if a user puts in an empty string I want the system to log an error such as ArgumentNullException.

string inputDirectory = "";
private void DoSomething(string inputDirectory)
            {
                try
                {
                    Directory.CreateDirectory(inputDirectory)
                }
                catch (ArgumentNullException e)
                {
                    Log.Error("program failed because the directory supplied was empty", e.Message);
                }
            }

The code is somewhere along these lines. Now the problem I have is the exception does not get thrown. Instead the program assumes that the directory is in the bin\Debug folder of the project. I am not sure what I need to do to stop execution of the program if the directory provided is "". I have done the if(inputDirectory == null) but this has not worked. Any advice? Thanks, Jetnor.

4

There are 4 answers

1
Sam Jenkins On BEST ANSWER

Perhaps you can add a check like;

string inputDirectory = "";
private void DoSomething(string inputDirectory)
{
    if (String.IsNullOrEmpty(inputDirectory)
        throw new ArgumentNullException();

    try
    {
        Directory.CreateDirectory(inputDirectory)
    }
    catch (ArgumentNullException e)
    {
        Log.Error("program failed because the directory supplied was empty", e.Message);
    }
}
0
Habib On

I am not sure what I need to do to stop execution of the program if the directory provided is "". I have done the if(inputDirectory == null) but this has not worked.

Use string.IsNullOrEmpty

Or if you are using .Net 4.0 or higher, you can use string.IsNullOrWhiteSpace

 if(string.IsNullOrWhiteSpace(inputDirectory))
 {
    //invalid input
 }

Both, string.IsNullOrEmpty and string.IsNullOrWhiteSpace would check against empty and null strings. string.IsNullOrWhiteSpace also checks for string containing all white spaces.

0
sloth On

You can use String.IsNullOrWhitespace to check if the string is, well, null or whitespace only.

Indicates whether a specified string is null, empty, or consists only of white-space characters.

if (String.IsNullOrWhitespace(inputDirectory))
{
    throw new YourException("WhatEver");
}
0
Sudhakar Tillapudi On

you need to use String.IsNullOrEmpty() for checking the Empty Strings.

method String.IsNullOrEmpty() checks wether given string is either null or Empty.

if it founds given String is either null or Empty then it returns true.

Step 1: Check inputDirectory is Null or Empty by using String.IsNullOrEmpty() method.
Step 2: if the method returns true throw the ArgumentNullException using throw keyword.

Try This:

            string inputDirectory = "";
            private void DoSomething(string inputDirectory)
            {
                try
                {
                    if(String.IsNullOrEmpty(inputDirectory))
                    throw new ArgumentNullException();
                    Directory.CreateDirectory(inputDirectory)
                }
                catch (ArgumentNullException e)
                {
                    Log.Error("program failed because the directory supplied was empty", e.Message);
                }
            }