Solved

Test if the filename is legal

Posted on 2007-11-20
2
544 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 500 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

707 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