How can I validate the folder name?
Posted on 2009-04-25
I am creating a folder in a form. The form has a textbox so that a user can enter a folder name.
As you know I am not allow to use / \ : < > ? " character as a part of the folder name.
So I coded as below.
//Create a new subfolder under the current active folder
string newPath = System.IO.Path.Combine(@"C:\MyFolder", newFolderName);
// Create the subfolder
MessageBox.Show("You can't use / \\ ? * < > : \" for the Folder name");
However, I found some problems here.
1) When / or \ is used as a first character, it doesn't catch them as invalid. If I enter // or \\, they are recognized as invalid.
2) When / or \ is followed by other characters, it doesn't catch them as invalid.
3) When : is entered as a first character, NotSupportedException is return at System.IO.Directory.CreateDirectory(newPath);
4) When : is followed by other characters, IOExcption is returned at System.IO.Directory.CreateDirectory(newPath);
What should I do to fix these problem?