Recursiv directory creation.
string CreateDirectory(string DirectoryPath, bool EndsWithBackslash)
{
while (DirectoryPath.EndsWith("\\"))
DirectoryPath = DirectoryPath.Remove(DirectoryPath.Length-1);
if (!Directory.Exists(DirectoryPath))
{
Directory.CreateDirectory(DirectoryPath);
}
else
{
int indexBracket1 = DirectoryPath.IndexOf("(");
int indexBracket2;
string Number = string.Empty;
if (indexBracket1 > -1)
{
indexBracket2 = DirectoryPath.IndexOf(")", indexBracket1);
Number = DirectoryPath.Substring(indexBracket1 + 1, indexBracket2 - indexBracket1 - 1);
}
if (indexBracket1 < 0)
{
DirectoryPath = DirectoryPath + "(2)";
DirectoryPath = CreateDirectory(DirectoryPath, EndsWithBackslash);
}
else
{
DirectoryPath = DirectoryPath.Replace("(" + Number + ")", "(" + (Convert.ToInt64(Number) + 1) + ")");
DirectoryPath = CreateDirectory(DirectoryPath, EndsWithBackslash);
}
}
if (EndsWithBackslash && !DirectoryPath.EndsWith("\\"))
DirectoryPath += @"\";
return DirectoryPath;
}
void CreateDirectory(ref string DirectoryPath, bool EndsWithBackslash)
{
DirectoryPath = CreateDirectory(DirectoryPath, EndsWithBackslash);
}
3 Kommentare zum Snippet