?
Solved

Test if the filename is legal

Posted on 2007-11-20
2
Medium Priority
?
545 Views
Last Modified: 2013-11-07
I am building a filename on some text fields so if the user put in the wrong signs the filename will be illegal.

Is there a build in function in C# to test if the filename is legal?
If someone have a few lines of code that can do the same it will be helpfull as well.

/Peter
0
Comment
Question by:PeterSvendsen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 7

Expert Comment

by:bungHoc
ID: 20318637
- Get all invalid path characters
- Use Regular Expressions to check if file name is legit


char[] invalidPathChars = System.IO.Path.GetInvalidPathChars();
string invalid = Regex.Escape (new string (invalidPathChars));
string validFileName = Regex.Replace (input, "[" + invalid + "]", "");

0
 
LVL 7

Accepted Solution

by:
bungHoc earned 2000 total points
ID: 20318660
This should work better:
private bool isValidFilename(string filename)
{
  char[] invalidFileChars = Path.GetInvalidFileNameChars();
  return (filename.IndexOfAny(invalidFileChars) < 0);
}
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

764 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