[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How can I validate the folder name?

Posted on 2009-04-25
4
Medium Priority
?
2,102 Views
Last Modified: 2012-05-06
Hi

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.
 try
            {
                //Create a new subfolder under the current active folder
                string newPath = System.IO.Path.Combine(@"C:\MyFolder", newFolderName);
                // Create the subfolder
                System.IO.Directory.CreateDirectory(newPath);
            }
            catch (ArgumentException)
            {
                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?
0
Comment
Question by:IzzyTwinkly
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:Aleksei_Malkov
ID: 24234740
Use Path.GetInvalidFileNameChars method. It returns array of chars that are not valid for file (directory) name.
Code will be something like below.

throwing and catching exception takes a lot of resources, and may interfere debugging.
foreach(char ch in Path.GetInvalidFileNameChars())
{
   if (newFolderName.Contains(ch))
{
   MessageBox("Invalid Charachter in name");
}
}

Open in new window

0
 

Author Comment

by:IzzyTwinkly
ID: 24234796
I thought about this kinda method, but I should have a way to execute following things.

Check if newFolderName has any invalid character.  
If it does, display the error message and don't create a folder.
else create a folder.

How can I achieve this with that foreach statment?
0
 
LVL 3

Accepted Solution

by:
Aleksei_Malkov earned 2000 total points
ID: 24234834
here is the modified source
// after foreach this variable will be true if the new folder name doesn't contain invalid charachters
 
bool validName = true
foreach(char ch in Path.GetInvalidFileNameChars())
{
   if (newFolderName.Contains(ch))
   {
       validName = false;
       break;
   }
}
 
if (validName && !string.IsNullOrEmpty(newFolderName))
{
   string newPath = System.IO.Path.Combine(@"C:\MyFolder", newFolderName);
 
   // Create the subfolder
   System.IO.Directory.CreateDirectory(newPath);
}
else
{
   MessageBox.Show("You can't use / \\ ? * < > : \" for the Folder name");
}

Open in new window

0
 

Author Comment

by:IzzyTwinkly
ID: 24234894
duh~  Thanks! =)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question