Obtaining a legal filename


using System;
using System.Text.RegularExpressions;
class Program
{
    static void Main(string[] args)
    {

         string input = "abc.txt"; 
    
         char[] invalidChars = System.IO.Path.GetInvalidPathChars(); 
         string invalidString = Regex.Escape (new string (invalidChars)); 
    
         string valid = Regex.Replace (input, "[" + invalidString + "]", ""); 
         Console.WriteLine (valid); 

    }
}

The output:


abc.txt
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.