FileSystem.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. foreach (var c in InvalidFileNameChars)
  99. {
  100. filename = filename.Replace(c, SpaceChar);
  101. }
  102. return filename;
  103. }
  104. /// <summary>
  105. /// Resolves the shortcut.
  106. /// </summary>
  107. /// <param name="filename">The filename.</param>
  108. /// <returns>System.String.</returns>
  109. /// <exception cref="System.ArgumentNullException">filename</exception>
  110. public static string ResolveShortcut(string filename)
  111. {
  112. if (string.IsNullOrEmpty(filename))
  113. {
  114. throw new ArgumentNullException("filename");
  115. }
  116. var link = new ShellLink();
  117. ((IPersistFile)link).Load(filename, NativeMethods.STGM_READ);
  118. // TODO: if I can get hold of the hwnd call resolve first. This handles moved and renamed files.
  119. // ((IShellLinkW)link).Resolve(hwnd, 0)
  120. var sb = new StringBuilder(NativeMethods.MAX_PATH);
  121. WIN32_FIND_DATA data;
  122. ((IShellLinkW)link).GetPath(sb, sb.Capacity, out data, 0);
  123. return sb.ToString();
  124. }
  125. /// <summary>
  126. /// Creates a shortcut file pointing to a specified path
  127. /// </summary>
  128. /// <param name="shortcutPath">The shortcut path.</param>
  129. /// <param name="target">The target.</param>
  130. /// <exception cref="System.ArgumentNullException">shortcutPath</exception>
  131. public static void CreateShortcut(string shortcutPath, string target)
  132. {
  133. if (string.IsNullOrEmpty(shortcutPath))
  134. {
  135. throw new ArgumentNullException("shortcutPath");
  136. }
  137. if (string.IsNullOrEmpty(target))
  138. {
  139. throw new ArgumentNullException("target");
  140. }
  141. var link = new ShellLink();
  142. ((IShellLinkW)link).SetPath(target);
  143. ((IPersistFile)link).Save(shortcutPath, true);
  144. }
  145. /// <summary>
  146. /// Determines whether the specified filename is shortcut.
  147. /// </summary>
  148. /// <param name="filename">The filename.</param>
  149. /// <returns><c>true</c> if the specified filename is shortcut; otherwise, <c>false</c>.</returns>
  150. /// <exception cref="System.ArgumentNullException">filename</exception>
  151. public static bool IsShortcut(string filename)
  152. {
  153. if (string.IsNullOrEmpty(filename))
  154. {
  155. throw new ArgumentNullException("filename");
  156. }
  157. return string.Equals(Path.GetExtension(filename), ".lnk", StringComparison.OrdinalIgnoreCase);
  158. }
  159. /// <summary>
  160. /// Copies all.
  161. /// </summary>
  162. /// <param name="source">The source.</param>
  163. /// <param name="target">The target.</param>
  164. /// <exception cref="System.ArgumentNullException">source</exception>
  165. /// <exception cref="System.ArgumentException">The source and target directories are the same</exception>
  166. public static void CopyAll(string source, string target)
  167. {
  168. if (string.IsNullOrEmpty(source))
  169. {
  170. throw new ArgumentNullException("source");
  171. }
  172. if (string.IsNullOrEmpty(target))
  173. {
  174. throw new ArgumentNullException("target");
  175. }
  176. if (source.Equals(target, StringComparison.OrdinalIgnoreCase))
  177. {
  178. throw new ArgumentException("The source and target directories are the same");
  179. }
  180. // Check if the target directory exists, if not, create it.
  181. if (!Directory.Exists(target))
  182. {
  183. Directory.CreateDirectory(target);
  184. }
  185. foreach (var file in Directory.EnumerateFiles(source))
  186. {
  187. File.Copy(file, Path.Combine(target, Path.GetFileName(file)), true);
  188. }
  189. // Copy each subdirectory using recursion.
  190. foreach (var dir in Directory.EnumerateDirectories(source))
  191. {
  192. CopyAll(dir, Path.Combine(target, Path.GetFileName(dir)));
  193. }
  194. }
  195. /// <summary>
  196. /// Parses the ini file.
  197. /// </summary>
  198. /// <param name="path">The path.</param>
  199. /// <returns>NameValueCollection.</returns>
  200. public static NameValueCollection ParseIniFile(string path)
  201. {
  202. var values = new NameValueCollection();
  203. foreach (var line in File.ReadAllLines(path))
  204. {
  205. var data = line.Split('=');
  206. if (data.Length < 2) continue;
  207. var key = data[0];
  208. var value = data.Length == 2 ? data[1] : string.Join(string.Empty, data, 1, data.Length - 1);
  209. values[key] = value;
  210. }
  211. return values;
  212. }
  213. }
  214. }