PathManager.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Configuration;
  6. using System.IO;
  7. using System.Reflection;
  8. using System.Text.RegularExpressions;
  9. namespace Aitex.Common.Util
  10. {
  11. public class PathManager
  12. {
  13. /// <summary>
  14. /// Application directory
  15. /// </summary>
  16. /// <returns></returns>
  17. public static string GetAppDir()
  18. {
  19. return string.IsNullOrWhiteSpace(_appPath) ? _appPath = GetAppStartupDirectory() : _appPath;
  20. }
  21. /// <summary>
  22. /// Application config directory is relative to the directory of the currently running application.
  23. /// end with the "/"
  24. /// </summary>
  25. /// <returns></returns>
  26. public static string GetCfgDir()
  27. {
  28. return GetDirectory("Config");
  29. }
  30. /// <summary>
  31. /// Application's log directory is relative to the directory of the currently running application.
  32. /// </summary>
  33. /// <returns></returns>
  34. public static string GetLogDir()
  35. {
  36. return GetDirectory("Logs");
  37. }
  38. /// <summary>
  39. /// Application's process recipe directory
  40. /// </summary>
  41. /// <returns></returns>
  42. public static string GetRecipeDir()
  43. {
  44. return GetDirectory("Recipes");
  45. }
  46. /// <summary>
  47. /// Application's account file path
  48. /// </summary>
  49. /// <returns></returns>
  50. public static string GetAccountFilePath()
  51. {
  52. return GetDirectory("Account");
  53. }
  54. /// <summary>
  55. /// Application start up directory
  56. /// </summary>
  57. /// <returns></returns>
  58. static string GetAppStartupDirectory()
  59. {
  60. var startupPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
  61. string dir = Path.GetDirectoryName(startupPath);
  62. return dir;
  63. }
  64. public static string GetDirectory(string directoryPath)
  65. {
  66. var ret = Path.Combine(GetAppDir(), directoryPath);
  67. if (!ret.EndsWith(Path.DirectorySeparatorChar.ToString()))
  68. ret = ret + Path.DirectorySeparatorChar;
  69. if (!Directory.Exists(ret))
  70. Directory.CreateDirectory(ret);
  71. return ret;
  72. }
  73. public static bool IsValidDirectoryPath(string path)
  74. {
  75. if (string.IsNullOrWhiteSpace(path)) return false;
  76. var drive = DriveInfo.GetDrives().FirstOrDefault(p => p.Name == Path.GetPathRoot(path));
  77. if (drive == null || !drive.IsReady) return false;
  78. string pattern = @"^[a-zA-Z]:\\(?:[^\\:*?""<>|\r\n]+\\)*[^\\:*?""<>|\r\n]*$";
  79. Regex regex = new Regex(pattern);
  80. return regex.IsMatch(path);
  81. }
  82. static string _appPath;
  83. }
  84. }