IoUtility.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 
  2. /*WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW*\ ( ( ) )
  3. |/ \| ) ) _((_
  4. || (c) Wanzyee Studio < wanzyeestudio.blogspot.com > || ( ( |_ _ |=n
  5. |\ /| _____)) | ! ] U
  6. \.ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ./ (_(__(S) |___*/
  7. using System;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text.RegularExpressions;
  11. namespace WanzyeeStudio{
  12. /// <summary>
  13. /// Include some convenient methods to extend IO operation.
  14. /// </summary>
  15. public static class IoUtility{
  16. /// <summary>
  17. /// Determine if the path can be used to create a file or directory.
  18. /// </summary>
  19. ///
  20. /// <remarks>
  21. /// Optional to throw an exception message or just return <c>false</c> if invalid.
  22. /// A legal path might not be in good format, e.g., "C:dir\ //file" or "/\pc\share\\new.txt".
  23. /// But it's safe to pass to <c>Directory</c> or <c>FileInfo</c> to create.
  24. /// Path in situations below is invalid, even dangerous:
  25. /// 1. Nothing but empty or white-spaces, nowhere to go.
  26. /// 2. Starts with 3 slashes, this causes crash while system looking for parent directories.
  27. /// 3. Includes invalid chars, can't name a file.
  28. /// 4. A name in path starts or ends with space, we can't get the created file, even delete.
  29. /// </remarks>
  30. ///
  31. /// <returns><c>true</c> if is creatable; otherwise, <c>false</c>.</returns>
  32. /// <param name="path">Path.</param>
  33. /// <param name="exception">Flag to throw an exception or return <c>false</c>.</param>
  34. ///
  35. public static bool CheckCreatable(string path, bool exception = false){
  36. try{
  37. if(null == path || "" == path.Trim())
  38. throw new Exception("Path cannot be null, empty, or white-spaces only.");
  39. if(Regex.IsMatch(path, @"\A[\\/]{3}"))
  40. throw new Exception("Path cannot start with 3 slashes.");
  41. var _c = Regex.IsMatch(path, @"\A\w:") ? path[0] + path.Substring(2) : path;
  42. _c = Regex.Replace(_c, @"[\\/]", "");
  43. if(-1 != _c.IndexOfAny(Path.GetInvalidFileNameChars()))
  44. throw new Exception("Illegal characters in path.");
  45. if(path.Split('\\', '/').Any(_v => Regex.IsMatch(_v, @"(^\s+\S|\S\s+$)")))
  46. throw new Exception("Directory or file name cannot start or end with white-space.");
  47. return true;
  48. }catch(Exception e){
  49. if(!exception) return false;
  50. throw new ArgumentException(e.Message, "path");
  51. }
  52. }
  53. /// <summary>
  54. /// Try to delete a file or directory at the specified path.
  55. /// </summary>
  56. ///
  57. /// <remarks>
  58. /// This doesn't work in Web Player.
  59. /// Note, the operation is permanently and irreversibly.
  60. /// Optional to trace and delete ancestor directories if became empty.
  61. /// </remarks>
  62. ///
  63. /// <param name="path">Path.</param>
  64. /// <param name="ancestor">If set to <c>true</c> delete ancestor directories if empty.</param>
  65. ///
  66. public static void Delete(string path, bool ancestor = false){
  67. #if UNITY_WEBPLAYER
  68. throw new NotSupportedException("'Delete' is not supported in Web Player.");
  69. #else
  70. try{
  71. var _f = new FileInfo(path);
  72. if(_f.Exists) _f.Delete();
  73. var _d = new DirectoryInfo(path);
  74. if(_d.Exists && null != _d.Parent) _d.Delete(true);
  75. if(!ancestor) return;
  76. for(var p = _d.Parent; null != p; p = p.Parent){
  77. if(p.Exists && null != p.Parent) p.Delete(false);
  78. }
  79. }catch{}
  80. #endif
  81. }
  82. }
  83. }