DirectoryItem.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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("The directory added to DirectoryItem was not found!", directoryPath);
  29. }
  30. m_directoryPath = directoryPath;
  31. FileInfo fileInfo = new FileInfo(m_directoryPath);
  32. displayName = fileInfo.Name;
  33. //
  34. // Get all the files in the directory
  35. //
  36. string[] files = Directory.GetFiles(m_directoryPath);
  37. foreach (string file in files)
  38. {
  39. mediaItems.Add(new FileItem(file));
  40. }
  41. //
  42. // Get all the subdirectories
  43. //
  44. string[] directories = Directory.GetDirectories(m_directoryPath);
  45. foreach (string directory in directories)
  46. {
  47. mediaItems.Add(new DirectoryItem(directory));
  48. }
  49. //
  50. // Get the Directory icon
  51. //
  52. SHFILEINFO shinfo = new SHFILEINFO();
  53. IntPtr hImg = Win32.SHGetFileInfo(m_directoryPath, 0, ref shinfo,
  54. (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
  55. if (shinfo.hIcon != null)
  56. {
  57. //The icon is returned in the hIcon member of the shinfo struct
  58. System.Drawing.IconConverter imageConverter = new System.Drawing.IconConverter();
  59. System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
  60. try
  61. {
  62. fileIconImage = (System.Drawing.Image)
  63. imageConverter.ConvertTo(icon, typeof(System.Drawing.Image));
  64. }
  65. catch (NotSupportedException)
  66. {
  67. }
  68. Win32.DestroyIcon(shinfo.hIcon);
  69. }
  70. }
  71. /// <summary>
  72. ///
  73. /// </summary>
  74. public string Path
  75. {
  76. get
  77. {
  78. return m_directoryPath;
  79. }
  80. }
  81. private string m_directoryPath;
  82. /// <summary>
  83. ///
  84. /// </summary>
  85. public override string ToString()
  86. {
  87. return displayName;
  88. }
  89. private string displayName;
  90. /// <summary>
  91. ///
  92. /// </summary>
  93. public Int64 SizeOnDisc
  94. {
  95. get
  96. {
  97. Int64 totalSize = 0;
  98. foreach (IMediaItem mediaItem in mediaItems)
  99. {
  100. totalSize += mediaItem.SizeOnDisc;
  101. }
  102. return totalSize;
  103. }
  104. }
  105. /// <summary>
  106. ///
  107. /// </summary>
  108. public System.Drawing.Image FileIconImage
  109. {
  110. get
  111. {
  112. return fileIconImage;
  113. }
  114. }
  115. private System.Drawing.Image fileIconImage = null;
  116. /// <summary>
  117. ///
  118. /// </summary>
  119. /// <param name="rootItem"></param>
  120. /// <returns></returns>
  121. public bool AddToFileSystem(IFsiDirectoryItem rootItem)
  122. {
  123. try
  124. {
  125. rootItem.AddTree(m_directoryPath, true);
  126. return true;
  127. }
  128. catch (Exception ex)
  129. {
  130. MessageBox.Show(ex.Message, "Error adding folder",
  131. MessageBoxButtons.OK,
  132. MessageBoxIcon.Error);
  133. return false;
  134. }
  135. }
  136. }
  137. }