Function to auto generate the new folder name.

Code Snippet

using System.IO;

class FileManager
	public static string GetNewFolderName(string currentFolder) 
		int newFldCount = Directory.GetDirectories(currentFolder, "New folder*", SearchOption.TopDirectoryOnly).Length;
		return string.Format("New folder{0}", (newFldCount > 0) ? string.Format(" ({0})", newFldCount + 1) : string.Empty);

Sample Usage

string newFolderName = FileManager.GetNewFolderName(@"C:\MyFolder");


If there is no folder named "New folder", output will be "New folder". If it is already exist, then output will be "New folder (2)", "New folder (3)", "New folder (4)" and so on ...

Categories : Programming, C#
Tags : Folder, IO, File Operation

author's photo

Author : Blue Cloud

Just a normal software developer who love to write code. Developing professional software since 1999. After working in the software industry for many years, I've started my own website to share knowledges and experiences.

blog comments powered by Disqus