views:

68

answers:

1

Im sarching how to create a safe filename in my webapplication I had read a lot of post around here last one http://stackoverflow.com/questions/62771/how-check-if-given-string-is-legal-allowed-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, '-'); }
A: 

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
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
@ncubica: Thanks for the accept. Only 15 rep from 2000!
Callum Rogers