简体   繁体   中英

Creating directory if directory exists

I have the following directory:

\\\\192.168.255.86\\Add-in\\Requests\\MyFolder1

If this directory exists (there is already a folder on the specified path) I need to create the following:

\\\\192.168.255.86\\Add-in\\Requests\\MyFolder1 (1)

If directory still exists I need to create another directory:

\\\\192.168.255.86\\Add-in\\Requests\\MyFolder1 (2)

and so on. I did it using while-loop in the following method:

public static string CreateDirectory(string path)
    {
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
            return path;
        }
        int i = 1;
        while (Directory.Exists(path + $" ({i})"))
        {
            i++;
        }
        path += $" ({i})";
        Directory.CreateDirectory(path);
        return path;
    }

How to make it using recursion?

You don't need recursion here. All you want is create new directory with next available name (by adding number).

A slightly refactored method can looks like this:

public string NextDirectory(string path)
{
    var dir = path;
    int n = 1;
    while (Directory.Exists(dir))
        dir = $"{path} ({n++})";
    Directory.CreateDirectory(dir);
    return dir;
}

If you insist on using recursion, this should be nice and elegant:

    public static string CreateDirectory(string path, int suffix = 0)
    {
        string directoryPath = DirectoryPath(path, suffix);
        if (!CreateDirectory(directoryPath))
            return CreateDirectory(path, i + 1);
        return directoryPath;
    }

    private static bool CreateDirectory(string path)
    {
        if (Directory.Exists(path))
            return false;

        Directory.CreateDirectory(path);
        return true;
    }

    private static string DirectoryPath(string path, int suffix)
    {
        return $"{path}{(suffix > 0 ? $" ({suffix})" : string.Empty)}";
    }

But if you already have 'MyFolder1 (214)' your call stack might be immense!

Maybe this is a slightly neater way to do the while loop (but essentially the same):

    public static string CreateDirectory(string path)
    {
        string createPath = GetUniquePath(path);
        Directory.CreateDirectory(createPath);
        return createPath;
    }
    private static string GetUniquePath(string path)
    {
        string result = path;
        int i = 1;
        while (Directory.Exists(result))
            result = $"{path} ({i++})";
        return result;
    }

You can use this:

  public static string CreateDirectory(string path, int i)
    {
        if (i>0)
        {
            if (Directory.Exists(path + $" ({i})"))
                return CreateDirectory(path,++i);
            else
            {
                Directory.CreateDirectory(path + $" ({i})"));
                return path + $" ({i})";
            }
        }
        else
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                return path;
            }
            else
                return CreateDirectory(path,1);    
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM