You donβt need recursion for this, but instead you need to look for an iterative solution
private void NewFolder(string path) { string name = @"\New Folder"; string current = name; int i = 0; while (Directory.Exists(Path.Combine(path, current)) { i++; current = String.Format("{0} {1}", name, i); } Directory.CreateDirectory(Path.Combine(path, current)); }
source share