2

Im sarching how to create a safe filename in my webapplication I had read a lot of post around here last one How do I check if a given string is a legal / valid file name under Windows? but I can't find the solution for this example when the filename comes in the way "fileName ..pdf" (double dot), the browser can't open the file at least IE and is a safe filename at least for windows,so how can I search for this exception and remove the double dot, what I have by now is the following example (which obviously doesn't remove the double dot:

foreach (var c in Path.GetInvalidFileNameChars()) { fileName = fileName.Replace(c, '-'); }
Community
  • 1
  • 1
ncubica
  • 25
  • 1
  • 4

1 Answers1

1

I would do an extra check afterwards and replace the .. with .:

foreach (var c in Path.GetInvalidFileNameChars())
    fileName = fileName.Replace(c, '-');

fileName = fileName.Replace("..", ".");
Callum Rogers
  • 15,630
  • 17
  • 67
  • 90
  • yes in dit, I thougth would be a problem if the file path have an dot in the middle like name.filenam.pdf but that is not a problem, the browser can open the file without problems, I guess this easy approach works thanks – ncubica Sep 23 '10 at 19:36