Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Test if the filename is legal

Posted on 2007-11-20
2
Medium Priority
?
546 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 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