Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static readonly char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
- // Builds a string out of valid chars
- var validFilename = new string(filename.Where(ch => !invalidFileNameChars.Contains(ch)).ToArray());
- static readonly char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
- // Builds a string out of valid chars and an _ for invalid ones
- var validFilename = new string(filename.Select(ch => invalidFileNameChars.Contains(ch) ? '_' : ch).ToArray());
- static readonly IList<char> invalidFileNameChars = Path.GetInvalidFileNameChars();
- // Builds a string out of valid chars and replaces invalid chars with a unique letter (Moves the Char into the letter range of unicode, starting at "A")
- var validFilename = new string(filename.Select(ch => invalidFileNameChars.Contains(ch) ? Convert.ToChar(invalidFileNameChars.IndexOf(ch) + 65) : ch).ToArray());
- string file = @"38?/.}[+=n a882 a.a*/|n^%$ ad#(-))";
- Array.ForEach(Path.GetInvalidFileNameChars(),
- c => file = file.Replace(c.ToString(), String.Empty));
- public static string MakeSafeFilename(string filename, char replaceChar)
- {
- foreach (char c in System.IO.Path.GetInvalidFileNameChars())
- {
- filename = filename.Replace(c, replaceChar);
- }
- return filename;
- }
- static string GetSafeFileName(string name, char replace = '_') {
- char[] invalids = Path.GetInvalidFileNameChars();
- return new string(name.Select(c => invalids.Contains(c) ? replace : c).ToArray());
- }
- string myCrazyName = "q`w^e!r@t#y$u%i^o&p*a(s)d_f-g+h=j{k}l|z:x"c<v>b?n[m]q\w;e'r,t.y/u";
- string safeName = Regex.Replace(
- myCrazyName,
- "W", /*Matches any nonword character. Equivalent to '[^A-Za-z0-9_]'*/
- "",
- RegexOptions.IgnoreCase);
- // safeName == "qwertyuiopasd_fghjklzxcvbnmqwertyu"
- static class Utils
- {
- public static string MakeFileSystemSafe(this string s)
- {
- return new string(s.Where(IsFileSystemSafe).ToArray());
- }
- public static bool IsFileSystemSafe(char c)
- {
- return !Path.GetInvalidFileNameChars().Contains(c);
- }
- }
- public static string ReplaceInvalidFileNameChars(this string s, string replacement = "")
- {
- return Regex.Replace(s,
- "[" + Regex.Escape(new String(System.IO.Path.GetInvalidPathChars())) + "]",
- replacement, //can even use a replacement string of any length
- RegexOptions.IgnoreCase);
- //not using System.IO.Path.InvalidPathChars (deprecated insecure API)
- }
- private void textBoxFileName_KeyPress(object sender, KeyPressEventArgs e)
- {
- e.Handled = CheckFileNameSafeCharacters(e);
- }
- /// <summary>
- /// This is a good function for making sure that a user who is naming a file uses proper characters
- /// </summary>
- /// <param name="e"></param>
- /// <returns></returns>
- internal static bool CheckFileNameSafeCharacters(System.Windows.Forms.KeyPressEventArgs e)
- {
- if (e.KeyChar.Equals(24) ||
- e.KeyChar.Equals(3) ||
- e.KeyChar.Equals(22) ||
- e.KeyChar.Equals(26) ||
- e.KeyChar.Equals(25))//Control-X, C, V, Z and Y
- return false;
- if (e.KeyChar.Equals('b'))//backspace
- return false;
- char[] charArray = Path.GetInvalidFileNameChars();
- if (charArray.Contains(e.KeyChar))
- return true;//Stop the character from being entered into the control since it is non-numerical
- else
- return false;
- }
- <Extension()>
- Public Function MakeSafeFileName(FileName As String) As String
- Return FileName.Where(Function(x) Not IO.Path.GetInvalidFileNameChars.Contains(x)).ToArray
- End Function
- string UnsafeFileName = "salmnas dlajhdla kjha;dmas'lkasn";
- string SafeFileName = Convert.ToBase64String(Encoding.UTF8.GetBytes(UnsafeFileName));
- UnsafeFileName = Encoding.UTF8.GetString(Convert.FromBase64String(SafeFileName));
Add Comment
Please, Sign In to add comment