FileSystem.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using MediaBrowser.Model.Logging;
  2. using System;
  3. using System.Collections.Specialized;
  4. using System.IO;
  5. using System.Text;
  6. namespace MediaBrowser.Controller.IO
  7. {
  8. /// <summary>
  9. /// Class FileSystem
  10. /// </summary>
  11. public static class FileSystem
  12. {
  13. /// <summary>
  14. /// Gets the file system info.
  15. /// </summary>
  16. /// <param name="path">The path.</param>
  17. /// <returns>FileSystemInfo.</returns>
  18. public static FileSystemInfo GetFileSystemInfo(string path)
  19. {
  20. // Take a guess to try and avoid two file system hits, but we'll double-check by calling Exists
  21. if (Path.HasExtension(path))
  22. {
  23. var fileInfo = new FileInfo(path);
  24. if (fileInfo.Exists)
  25. {
  26. return fileInfo;
  27. }
  28. return new DirectoryInfo(path);
  29. }
  30. else
  31. {
  32. var fileInfo = new DirectoryInfo(path);
  33. if (fileInfo.Exists)
  34. {
  35. return fileInfo;
  36. }
  37. return new FileInfo(path);
  38. }
  39. }
  40. /// <summary>
  41. /// Gets the creation time UTC.
  42. /// </summary>
  43. /// <param name="info">The info.</param>
  44. /// <param name="logger">The logger.</param>
  45. /// <returns>DateTime.</returns>
  46. public static DateTime GetLastWriteTimeUtc(FileSystemInfo info, ILogger logger)
  47. {
  48. // This could throw an error on some file systems that have dates out of range
  49. try
  50. {
  51. return info.LastWriteTimeUtc;
  52. }
  53. catch (Exception ex)
  54. {
  55. logger.ErrorException("Error determining LastAccessTimeUtc for {0}", ex, info.FullName);
  56. return DateTime.MinValue;
  57. }
  58. }
  59. /// <summary>
  60. /// Gets the creation time UTC.
  61. /// </summary>
  62. /// <param name="info">The info.</param>
  63. /// <param name="logger">The logger.</param>
  64. /// <returns>DateTime.</returns>
  65. public static DateTime GetCreationTimeUtc(FileSystemInfo info, ILogger logger)
  66. {
  67. // This could throw an error on some file systems that have dates out of range
  68. try
  69. {
  70. return info.CreationTimeUtc;
  71. }
  72. catch (Exception ex)
  73. {
  74. logger.ErrorException("Error determining CreationTimeUtc for {0}", ex, info.FullName);
  75. return DateTime.MinValue;
  76. }
  77. }
  78. /// <summary>
  79. /// The space char
  80. /// </summary>
  81. private const char SpaceChar = ' ';
  82. /// <summary>
  83. /// The invalid file name chars
  84. /// </summary>
  85. private static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars();
  86. /// <summary>
  87. /// Takes a filename and removes invalid characters
  88. /// </summary>
  89. /// <param name="filename">The filename.</param>
  90. /// <returns>System.String.</returns>
  91. /// <exception cref="System.ArgumentNullException">filename</exception>
  92. public static string GetValidFilename(string filename)
  93. {
  94. if (string.IsNullOrEmpty(filename))
  95. {
  96. throw new ArgumentNullException("filename");
  97. }
  98. var builder = new StringBuilder(filename);
  99. foreach (var c in InvalidFileNameChars)
  100. {
  101. builder = builder.Replace(c, SpaceChar);
  102. }
  103. return builder.ToString();
  104. }
  105. /// <summary>
  106. /// Resolves the shortcut.
  107. /// </summary>
  108. /// <param name="filename">The filename.</param>
  109. /// <returns>System.String.</returns>
  110. /// <exception cref="System.ArgumentNullException">filename</exception>
  111. public static string ResolveShortcut(string filename)
  112. {
  113. if (string.IsNullOrEmpty(filename))
  114. {
  115. throw new ArgumentNullException("filename");
  116. }
  117. var link = new ShellLink();
  118. ((IPersistFile)link).Load(filename, NativeMethods.STGM_READ);
  119. // TODO: if I can get hold of the hwnd call resolve first. This handles moved and renamed files.
  120. // ((IShellLinkW)link).Resolve(hwnd, 0)
  121. var sb = new StringBuilder(NativeMethods.MAX_PATH);
  122. WIN32_FIND_DATA data;
  123. ((IShellLinkW)link).GetPath(sb, sb.Capacity, out data, 0);
  124. return sb.ToString();
  125. }
  126. /// <summary>
  127. /// Creates a shortcut file pointing to a specified path
  128. /// </summary>
  129. /// <param name="shortcutPath">The shortcut path.</param>
  130. /// <param name="target">The target.</param>
  131. /// <exception cref="System.ArgumentNullException">shortcutPath</exception>
  132. public static void CreateShortcut(string shortcutPath, string target)
  133. {
  134. if (string.IsNullOrEmpty(shortcutPath))
  135. {
  136. throw new ArgumentNullException("shortcutPath");
  137. }
  138. if (string.IsNullOrEmpty(target))
  139. {
  140. throw new ArgumentNullException("target");
  141. }
  142. var link = new ShellLink();
  143. ((IShellLinkW)link).SetPath(target);
  144. ((IPersistFile)link).Save(shortcutPath, true);
  145. }
  146. /// <summary>
  147. /// Determines whether the specified filename is shortcut.
  148. /// </summary>
  149. /// <param name="filename">The filename.</param>
  150. /// <returns><c>true</c> if the specified filename is shortcut; otherwise, <c>false</c>.</returns>
  151. /// <exception cref="System.ArgumentNullException">filename</exception>
  152. public static bool IsShortcut(string filename)
  153. {
  154. if (string.IsNullOrEmpty(filename))
  155. {
  156. throw new ArgumentNullException("filename");
  157. }
  158. return string.Equals(Path.GetExtension(filename), ".lnk", StringComparison.OrdinalIgnoreCase);
  159. }
  160. /// <summary>
  161. /// Copies all.
  162. /// </summary>
  163. /// <param name="source">The source.</param>
  164. /// <param name="target">The target.</param>
  165. /// <exception cref="System.ArgumentNullException">source</exception>
  166. /// <exception cref="System.ArgumentException">The source and target directories are the same</exception>
  167. public static void CopyAll(string source, string target)
  168. {
  169. if (string.IsNullOrEmpty(source))
  170. {
  171. throw new ArgumentNullException("source");
  172. }
  173. if (string.IsNullOrEmpty(target))
  174. {
  175. throw new ArgumentNullException("target");
  176. }
  177. if (source.Equals(target, StringComparison.OrdinalIgnoreCase))
  178. {
  179. throw new ArgumentException("The source and target directories are the same");
  180. }
  181. // Check if the target directory exists, if not, create it.
  182. Directory.CreateDirectory(target);
  183. foreach (var file in Directory.EnumerateFiles(source))
  184. {
  185. File.Copy(file, Path.Combine(target, Path.GetFileName(file)), true);
  186. }
  187. // Copy each subdirectory using recursion.
  188. foreach (var dir in Directory.EnumerateDirectories(source))
  189. {
  190. CopyAll(dir, Path.Combine(target, Path.GetFileName(dir)));
  191. }
  192. }
  193. /// <summary>
  194. /// Parses the ini file.
  195. /// </summary>
  196. /// <param name="path">The path.</param>
  197. /// <returns>NameValueCollection.</returns>
  198. public static NameValueCollection ParseIniFile(string path)
  199. {
  200. var values = new NameValueCollection();
  201. foreach (var line in File.ReadAllLines(path))
  202. {
  203. var data = line.Split('=');
  204. if (data.Length < 2) continue;
  205. var key = data[0];
  206. var value = data.Length == 2 ? data[1] : string.Join(string.Empty, data, 1, data.Length - 1);
  207. values[key] = value;
  208. }
  209. return values;
  210. }
  211. }
  212. }