public void AddDirectory(string path)
{
var fixedPath = FixPath(path);
var separator = XFS.Separator();
lock (files)
{
if (FileExists(path) &&
(files[fixedPath].Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.ACCESS_TO_THE_PATH_IS_DENIED, path));
var lastIndex = 0;
bool isUnc =
path.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase) ||
path.StartsWith(@"//", StringComparison.OrdinalIgnoreCase);
if (isUnc)
{
//First, confirm they aren't trying to create '\\server\'
lastIndex = path.IndexOf(separator, 2, StringComparison.OrdinalIgnoreCase);
if (lastIndex < 0)
throw new ArgumentException(@"The UNC path should be of the form \\server\share.", "path");
/*
* Although CreateDirectory(@"\\server\share\") is not going to work in real code, we allow it here for the purposes of setting up test doubles.
* See PR https://github.com/tathamoddie/System.IO.Abstractions/pull/90 for conversation
*/
}
while ((lastIndex = path.IndexOf(separator, lastIndex + 1, StringComparison.OrdinalIgnoreCase)) > -1)
{
var segment = path.Substring(0, lastIndex + 1);
if (!directory.Exists(segment))
{
files[segment] = new MockDirectoryData();
}
}
var s = path.EndsWith(separator, StringComparison.OrdinalIgnoreCase) ? path : path + separator;
files[s] = new MockDirectoryData();
}
}