DirectoryItem.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // DirectoryItem.cs
  3. //
  4. // by Eric Haddan
  5. //
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Runtime.InteropServices;
  10. using System.Windows.Forms;
  11. using IMAPI2.Interop;
  12. namespace IMAPI2.MediaItem
  13. {
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. class DirectoryItem : IMediaItem
  18. {
  19. private List<IMediaItem> mediaItems = new List<IMediaItem>();
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. /// <param name="directoryPath"></param>
  24. public DirectoryItem(string directoryPath)
  25. {
  26. if (!Directory.Exists(directoryPath))
  27. {
  28. throw new FileNotFoundException("加入DirectoryItem目录未找到!", directoryPath);
  29. }
  30. if (directoryPath.Length <= 3)
  31. {
  32. throw new FileNotFoundException("目录不能直接为驱动盘本身!", directoryPath);
  33. }
  34. m_directoryPath = directoryPath;
  35. FileInfo fileInfo = new FileInfo(m_directoryPath);
  36. displayName = fileInfo.Name;
  37. //
  38. // Get all the files in the directory
  39. //
  40. string[] files = Directory.GetFiles(m_directoryPath);
  41. foreach (string file in files)
  42. {
  43. mediaItems.Add(new FileItem(file));
  44. }
  45. //
  46. // Get all the subdirectories
  47. //
  48. string[] directories = Directory.GetDirectories(m_directoryPath);
  49. foreach (string directory in directories)
  50. {
  51. mediaItems.Add(new DirectoryItem(directory));
  52. }
  53. //
  54. // Get the Directory icon
  55. //
  56. SHFILEINFO shinfo = new SHFILEINFO();
  57. IntPtr hImg = Win32.SHGetFileInfo(m_directoryPath, 0, ref shinfo,
  58. (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
  59. if (shinfo.hIcon != null)
  60. {
  61. //The icon is returned in the hIcon member of the shinfo struct
  62. System.Drawing.IconConverter imageConverter = new System.Drawing.IconConverter();
  63. System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
  64. try
  65. {
  66. fileIconImage = (System.Drawing.Image)
  67. imageConverter.ConvertTo(icon, typeof(System.Drawing.Image));
  68. }
  69. catch (NotSupportedException)
  70. {
  71. }
  72. Win32.DestroyIcon(shinfo.hIcon);
  73. }
  74. }
  75. /// <summary>
  76. ///
  77. /// </summary>
  78. public string Path
  79. {
  80. get
  81. {
  82. return m_directoryPath;
  83. }
  84. }
  85. private string m_directoryPath;
  86. /// <summary>
  87. ///
  88. /// </summary>
  89. public override string ToString()
  90. {
  91. return displayName;
  92. }
  93. private string displayName;
  94. /// <summary>
  95. ///
  96. /// </summary>
  97. public Int64 SizeOnDisc
  98. {
  99. get
  100. {
  101. Int64 totalSize = 0;
  102. foreach (IMediaItem mediaItem in mediaItems)
  103. {
  104. totalSize += mediaItem.SizeOnDisc;
  105. }
  106. return totalSize;
  107. }
  108. }
  109. /// <summary>
  110. ///
  111. /// </summary>
  112. public System.Drawing.Image FileIconImage
  113. {
  114. get
  115. {
  116. return fileIconImage;
  117. }
  118. }
  119. private System.Drawing.Image fileIconImage = null;
  120. /// <summary>
  121. ///
  122. /// </summary>
  123. /// <param name="rootItem"></param>
  124. /// <returns></returns>
  125. public bool AddToFileSystem(IFsiDirectoryItem rootItem)
  126. {
  127. try
  128. {
  129. rootItem.AddTree(m_directoryPath, true);
  130. return true;
  131. }
  132. catch (Exception ex)
  133. {
  134. MessageBox.Show(ex.Message, "Error adding folder",
  135. MessageBoxButtons.OK,
  136. MessageBoxIcon.Error);
  137. return false;
  138. }
  139. }
  140. }
  141. }