User Id :    Password :      New Member   Forgot Password  
C# function to remove illegal characters in a filename
Description This function remove all the illegal characters from a filename and return a valid filename so that you can perform file handling operation smoothly.   No. of Views     6307
  Rating     0
Author Sumit Gupta   Posted On     03 Aug 2011
Tags C#,.NET Frameworks,.NET 4.0,General    

Sample Code   Download Code

Whenever we perform File Handling operation such as creating a file on disk so we need to generate a file name that can be generate on any logic. But some time that file name is not vaild and we got runtime errors.

So it is better to check the file name before creating the file.

I have written a small c# function to check and replace such illegal character to underscore from file name:

using System.IO;

 private static string CheckFileName(string fileName)
            foreach (char badChar in Path.GetInvalidFileNameChars())
                fileName = fileName.Replace(badChar, '_');
            return fileName;

Happy Coding..

About Author

About Author I am Sumit Gupta working in 3 Pillar Global Pvt. Ltd as Module Lead. I have 7+ year of experience in .Net technologies. I love to explore new technologies and write technical article. Sumit Gupta
No Photo
Country India
Company 3 Pillar Global Pvt. Ltd.
Home Page

Rate this article

Rating options from poor, fair, good, very good to excelent.  


Posted By Annonymous on 03 Sep 2011 at 04:56 AM
This website makes tihgns hella easy.
Write your comment here.
Verification Code