How to sanitize a file name using LINQ
Submitted by Tomasz Paprocki on 8 September, 2011 - 10:29
From this article you can learn how to sanitize a file name using simple LINQ query. SanitizeFilename function takes two parameters:
- name - filename that needs to be sanitized
- replaceChar - char used to replace all invalid file name characters
Implementation
System.Linq namespace needs to be put in usings.public static string SanitizeFileName(string name, char replaceChar) { return Path.GetInvalidFileNameChars().Aggregate(name, (current, c) => current.Replace(c, replaceChar)); }
Public Shared Function SanitizeFileName(name As String, replaceChar As Char) As String Return Path.GetInvalidFileNameChars().Aggregate(name, Function(current, c) current.Replace(c, replaceChar)) End Function
Comments
Post new comment