FileItem.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // FileItem.cs
  3. //
  4. // by Eric Haddan
  5. //
  6. using System;
  7. using System.IO;
  8. using System.Runtime.InteropServices;
  9. using System.Runtime.InteropServices.ComTypes;
  10. using System.Windows.Forms;
  11. using IMAPI2.Interop;
  12. namespace IMAPI2.MediaItem
  13. {
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. class FileItem : IMediaItem
  18. {
  19. private const Int64 SECTOR_SIZE = 2048;
  20. private Int64 m_fileLength = 0;
  21. public FileItem(string path)
  22. {
  23. if (!File.Exists(path))
  24. {
  25. throw new FileNotFoundException("未发现加入的FileItem文件!", path);
  26. }
  27. filePath = path;
  28. FileInfo fileInfo = new FileInfo(filePath);
  29. displayName = fileInfo.Name;
  30. m_fileLength = fileInfo.Length;
  31. //
  32. // Get the File icon
  33. //
  34. SHFILEINFO shinfo = new SHFILEINFO();
  35. IntPtr hImg = Win32.SHGetFileInfo(filePath, 0, ref shinfo,
  36. (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
  37. if (shinfo.hIcon != null)
  38. {
  39. //The icon is returned in the hIcon member of the shinfo struct
  40. System.Drawing.IconConverter imageConverter = new System.Drawing.IconConverter();
  41. System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
  42. try
  43. {
  44. fileIconImage = (System.Drawing.Image)
  45. imageConverter.ConvertTo(icon, typeof(System.Drawing.Image));
  46. }
  47. catch (NotSupportedException)
  48. {
  49. }
  50. Win32.DestroyIcon(shinfo.hIcon);
  51. }
  52. }
  53. /// <summary>
  54. ///
  55. /// </summary>
  56. public Int64 SizeOnDisc
  57. {
  58. get
  59. {
  60. if (m_fileLength > 0)
  61. {
  62. return ((m_fileLength / SECTOR_SIZE) + 1) * SECTOR_SIZE;
  63. }
  64. return 0;
  65. }
  66. }
  67. /// <summary>
  68. ///
  69. /// </summary>
  70. public string Path
  71. {
  72. get
  73. {
  74. return filePath;
  75. }
  76. }
  77. private string filePath;
  78. /// <summary>
  79. ///
  80. /// </summary>
  81. public System.Drawing.Image FileIconImage
  82. {
  83. get
  84. {
  85. return fileIconImage;
  86. }
  87. }
  88. private System.Drawing.Image fileIconImage = null;
  89. /// <summary>
  90. ///
  91. /// </summary>
  92. public override string ToString()
  93. {
  94. return displayName;
  95. }
  96. private string displayName;
  97. public bool AddToFileSystem(IFsiDirectoryItem rootItem)
  98. {
  99. IStream stream = null;
  100. try
  101. {
  102. Win32.SHCreateStreamOnFile(filePath, Win32.STGM_READ | Win32.STGM_SHARE_DENY_WRITE, ref stream);
  103. if (stream != null)
  104. {
  105. rootItem.AddFile(displayName, stream);
  106. return true;
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. MessageBox.Show(ex.Message, "文件添加错误",
  112. MessageBoxButtons.OK, MessageBoxIcon.Error);
  113. }
  114. finally
  115. {
  116. if (stream != null)
  117. {
  118. Marshal.FinalReleaseComObject(stream);
  119. }
  120. }
  121. return false;
  122. }
  123. }
  124. }