Reputation: 2757
I just encountered a situation where a colleague is creating directories from a list. It just so happens that one of the items in the list is null but an exception is not null.
To satisfy my curiosity, I wrote a test program to mimic what he was doing. This test programo attempts to create a null directory in c:\Temp, which already exists. I would've expected a Null Exception to be thrown but no exception was thrown.
Here's my test program.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace CreateNullDirectory
{
class Program
{
static void Main(string[] args)
{
String DirectoryName = null;
String FullDirectoryName = string.Format("c:\\Temp\\{0}",DirectoryName);
Console.WriteLine(string.Format("Creating Directory {0}", FullDirectoryName));
try
{
Directory.CreateDirectory(FullDirectoryName);
Console.WriteLine(string.Format("Successfully created directory {0}", FullDirectoryName));
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Error creating {0} - {1}", FullDirectoryName, ex.Message));
}
Console.ReadLine();
}
}
}
Here is the output:
Is CreateDirectory ignoring the terminating null and I'm only attempting to create C:\Temp which already exists?
Upvotes: 0
Views: 1695
Reputation: 21887
All you're doing is calling Directory.CreateDirectory("C:\Temp")
, because passing a null value in format string returns string.Empty
.
CreateDirectory
either creates the directory if it doesn't exist, or does nothing if it already exists.
Upvotes: 2