Solved

Test if the filename is legal

Posted on 2007-11-20
2
537 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now