Guest User

Untitled

a guest
Jan 26th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. static readonly char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
  2.  
  3. // Builds a string out of valid chars
  4. var validFilename = new string(filename.Where(ch => !invalidFileNameChars.Contains(ch)).ToArray());
  5.  
  6. static readonly char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
  7.  
  8. // Builds a string out of valid chars and an _ for invalid ones
  9. var validFilename = new string(filename.Select(ch => invalidFileNameChars.Contains(ch) ? '_' : ch).ToArray());
  10.  
  11. static readonly IList<char> invalidFileNameChars = Path.GetInvalidFileNameChars();
  12.  
  13. // 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")
  14. var validFilename = new string(filename.Select(ch => invalidFileNameChars.Contains(ch) ? Convert.ToChar(invalidFileNameChars.IndexOf(ch) + 65) : ch).ToArray());
  15.  
  16. string file = @"38?/.}[+=n a882 a.a*/|n^%$ ad#(-))";
  17. Array.ForEach(Path.GetInvalidFileNameChars(),
  18. c => file = file.Replace(c.ToString(), String.Empty));
  19.  
  20. public static string MakeSafeFilename(string filename, char replaceChar)
  21. {
  22. foreach (char c in System.IO.Path.GetInvalidFileNameChars())
  23. {
  24. filename = filename.Replace(c, replaceChar);
  25. }
  26. return filename;
  27. }
  28.  
  29. static string GetSafeFileName(string name, char replace = '_') {
  30. char[] invalids = Path.GetInvalidFileNameChars();
  31. return new string(name.Select(c => invalids.Contains(c) ? replace : c).ToArray());
  32. }
  33.  
  34. 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";
  35. string safeName = Regex.Replace(
  36. myCrazyName,
  37. "W", /*Matches any nonword character. Equivalent to '[^A-Za-z0-9_]'*/
  38. "",
  39. RegexOptions.IgnoreCase);
  40. // safeName == "qwertyuiopasd_fghjklzxcvbnmqwertyu"
  41.  
  42. static class Utils
  43. {
  44. public static string MakeFileSystemSafe(this string s)
  45. {
  46. return new string(s.Where(IsFileSystemSafe).ToArray());
  47. }
  48.  
  49. public static bool IsFileSystemSafe(char c)
  50. {
  51. return !Path.GetInvalidFileNameChars().Contains(c);
  52. }
  53. }
  54.  
  55. public static string ReplaceInvalidFileNameChars(this string s, string replacement = "")
  56. {
  57. return Regex.Replace(s,
  58. "[" + Regex.Escape(new String(System.IO.Path.GetInvalidPathChars())) + "]",
  59. replacement, //can even use a replacement string of any length
  60. RegexOptions.IgnoreCase);
  61. //not using System.IO.Path.InvalidPathChars (deprecated insecure API)
  62. }
  63.  
  64. private void textBoxFileName_KeyPress(object sender, KeyPressEventArgs e)
  65. {
  66. e.Handled = CheckFileNameSafeCharacters(e);
  67. }
  68.  
  69. /// <summary>
  70. /// This is a good function for making sure that a user who is naming a file uses proper characters
  71. /// </summary>
  72. /// <param name="e"></param>
  73. /// <returns></returns>
  74. internal static bool CheckFileNameSafeCharacters(System.Windows.Forms.KeyPressEventArgs e)
  75. {
  76. if (e.KeyChar.Equals(24) ||
  77. e.KeyChar.Equals(3) ||
  78. e.KeyChar.Equals(22) ||
  79. e.KeyChar.Equals(26) ||
  80. e.KeyChar.Equals(25))//Control-X, C, V, Z and Y
  81. return false;
  82. if (e.KeyChar.Equals('b'))//backspace
  83. return false;
  84.  
  85. char[] charArray = Path.GetInvalidFileNameChars();
  86. if (charArray.Contains(e.KeyChar))
  87. return true;//Stop the character from being entered into the control since it is non-numerical
  88. else
  89. return false;
  90. }
  91.  
  92. <Extension()>
  93. Public Function MakeSafeFileName(FileName As String) As String
  94. Return FileName.Where(Function(x) Not IO.Path.GetInvalidFileNameChars.Contains(x)).ToArray
  95. End Function
  96.  
  97. string UnsafeFileName = "salmnas dlajhdla kjha;dmas'lkasn";
  98. string SafeFileName = Convert.ToBase64String(Encoding.UTF8.GetBytes(UnsafeFileName));
  99.  
  100. UnsafeFileName = Encoding.UTF8.GetString(Convert.FromBase64String(SafeFileName));
Add Comment
Please, Sign In to add comment