// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Text.RegularExpressions; namespace E { public class Program { public static string SafeName(string fnm) { string res = fnm; MatchCollection reg = Regex.Matches(res, @"[^A-Za-z0-9-_\.]"); for(int i = 0; i < reg.Count; i++) { res = res.Replace(reg[i].Groups[0].Value, "__" + ((int)reg[i].Groups[0].Value[0]).ToString("X2")); } return res; } public static void Main(string[] args) { Console.WriteLine(SafeName("a.txt")); Console.WriteLine(SafeName("æøå.txt")); Console.WriteLine(SafeName(":::.txt")); } } }