1

I use this static function in c# on uploading a file to replace invalid file names by using RegEx :

static string removeBadChar(string filename)
{
   // Replace invalid characters with "_" char.
   return Regex.Replace(filename, @"[^\w\.-]", "_");    
}

And the File Name.csv is replaced with File_Name.csv

But I have another problem, if the file name of CSV expects these keyboard letters : é, è, à, ù, ì, ò the web application faults.

How can I resolve this?.

Daniel Casserly
  • 3,552
  • 2
  • 29
  • 60
Antonio Mailtraq
  • 1,397
  • 5
  • 34
  • 82
  • 4
    Why is a space a bad character? Could you elaborate on where this requirement comes from? How do you know the complete list of invalid characters? – Thomas Weller Nov 16 '15 at 13:31
  • Your regex should match your special chars also – Thomas Ayoub Nov 16 '15 at 13:31
  • What about the typical DOS invalid characters like `:`, `*`, `?`, `\\`, `/`, `|`, `"`, `<` and `>`? Also, AFAIK WebDAV does not allow `&`. – Thomas Weller Nov 16 '15 at 13:34
  • you can use this to get filename illegal characters https://msdn.microsoft.com/en-us/library/system.io.path.getinvalidfilenamechars%28v=vs.110%29.aspx all other characters you have to add yourself with a regular expression. – Denis Kralj Nov 16 '15 at 13:36

2 Answers2

0

You can use [\p{Lu}\p{Ll}]+ to match all uppercase and lowercase unicode letters.

Anirudha
  • 32,393
  • 7
  • 68
  • 89
0

Please see Replacing characters in C# (ASCII)

I hope to have been helpful in solving your problem.

Community
  • 1
  • 1
Hamamelis
  • 1,983
  • 8
  • 27
  • 41