Program.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using System.Threading;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. namespace LYFZ.NeroDiscBurn.NET
  9. {
  10. static class Program
  11. {
  12. /// <summary>
  13. /// 应用程序的主入口点。 UpdateSuccess
  14. /// </summary>
  15. [STAThread]
  16. static void Main(string[] args)
  17. {
  18. Application.EnableVisualStyles();
  19. Application.SetCompatibleTextRenderingDefault(false);
  20. Application.Run(new LYFZ.NeroDiscBurn.NET.BrunAPP.frmMain());
  21. }
  22. }
  23. //创建一个委托,是为访问DataGridVie控件服务的。
  24. public delegate void UpdateControl();
  25. public class ItemValue
  26. {
  27. private string _text = string.Empty;
  28. public string Text
  29. {
  30. get
  31. {
  32. if (this._text != null)
  33. {
  34. return this._text;
  35. }
  36. return string.Empty;
  37. }
  38. set { _text = value; }
  39. }
  40. private object _value = string.Empty;
  41. public object Value
  42. {
  43. get { return this._value; }
  44. set { this._value = value; }
  45. }
  46. /// <summary>
  47. ///
  48. /// </summary>
  49. /// <param name="_value"></param>
  50. /// <param name="_text"></param>
  51. public ItemValue(object value, string text)
  52. {
  53. this._value = value;
  54. this._text = text;
  55. }
  56. public override string ToString()
  57. {
  58. return this._text;
  59. }
  60. }
  61. [StructLayout(LayoutKind.Sequential)]
  62. public struct SHFILEINFO
  63. {
  64. public IntPtr hIcon;
  65. public IntPtr iIcon;
  66. public uint dwAttributes;
  67. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  68. public string szDisplayName;
  69. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
  70. public string szTypeName;
  71. };
  72. class Win32
  73. {
  74. public const uint SHGFI_ICON = 0x100;
  75. public const uint SHGFI_LARGEICON = 0x0; // Large icon
  76. public const uint SHGFI_SMALLICON = 0x1; // Small icon
  77. [DllImport("shell32.dll")]
  78. public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
  79. [DllImport("user32.dll")]
  80. public static extern bool DestroyIcon(IntPtr handle);
  81. public const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
  82. public const uint STGM_DELETEONRELEASE = 0x04000000;
  83. public const uint STGM_SHARE_DENY_WRITE = 0x00000020;
  84. public const uint STGM_SHARE_DENY_NONE = 0x00000040;
  85. public const uint STGM_READ = 0x00000000;
  86. [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true, PreserveSig = false, EntryPoint = "SHCreateStreamOnFileW")]
  87. public static extern void SHCreateStreamOnFile(string fileName, uint mode, ref System.Runtime.InteropServices.ComTypes.IStream stream);
  88. }
  89. /// <summary>
  90. ///
  91. /// </summary>
  92. public class FileItem : IMediaItem
  93. {
  94. private const Int64 SECTOR_SIZE = 2048;
  95. private Int64 m_fileLength = 0;
  96. public FileItem(string path)
  97. {
  98. if (!File.Exists(path))
  99. {
  100. throw new FileNotFoundException("未发现加入的FileItem文件!", path);
  101. }
  102. filePath = path;
  103. FileInfo fileInfo = new FileInfo(filePath);
  104. displayName = fileInfo.Name;
  105. m_fileLength = fileInfo.Length;
  106. //
  107. // Get the File icon
  108. //
  109. SHFILEINFO shinfo = new SHFILEINFO();
  110. IntPtr hImg = Win32.SHGetFileInfo(filePath, 0, ref shinfo,
  111. (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
  112. if (shinfo.hIcon != null)
  113. {
  114. //The icon is returned in the hIcon member of the shinfo struct
  115. System.Drawing.IconConverter imageConverter = new System.Drawing.IconConverter();
  116. System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
  117. try
  118. {
  119. fileIconImage = (System.Drawing.Image)
  120. imageConverter.ConvertTo(icon, typeof(System.Drawing.Image));
  121. }
  122. catch (NotSupportedException)
  123. {
  124. }
  125. Win32.DestroyIcon(shinfo.hIcon);
  126. }
  127. }
  128. /// <summary>
  129. ///
  130. /// </summary>
  131. public Int64 SizeOnDisc
  132. {
  133. get
  134. {
  135. if (m_fileLength > 0)
  136. {
  137. return ((m_fileLength / SECTOR_SIZE) + 1) * SECTOR_SIZE;
  138. }
  139. return 0;
  140. }
  141. }
  142. /// <summary>
  143. ///
  144. /// </summary>
  145. public string Path
  146. {
  147. get
  148. {
  149. return filePath;
  150. }
  151. }
  152. private string filePath;
  153. /// <summary>
  154. ///
  155. /// </summary>
  156. public System.Drawing.Image FileIconImage
  157. {
  158. get
  159. {
  160. return fileIconImage;
  161. }
  162. }
  163. private System.Drawing.Image fileIconImage = null;
  164. /// <summary>
  165. ///
  166. /// </summary>
  167. public override string ToString()
  168. {
  169. return displayName;
  170. }
  171. private string displayName;
  172. /* public bool AddToFileSystem(IFsiDirectoryItem rootItem)
  173. {
  174. System.Runtime.InteropServices.ComTypes.IStream stream = null;
  175. try
  176. {
  177. Win32.SHCreateStreamOnFile(filePath, Win32.STGM_READ | Win32.STGM_SHARE_DENY_WRITE, ref stream);
  178. if (stream != null)
  179. {
  180. rootItem.AddFile(displayName, stream);
  181. return true;
  182. }
  183. }
  184. catch (Exception ex)
  185. {
  186. MessageBox.Show(ex.Message, "文件添加错误",
  187. MessageBoxButtons.OK, MessageBoxIcon.Error);
  188. }
  189. finally
  190. {
  191. if (stream != null)
  192. {
  193. Marshal.FinalReleaseComObject(stream);
  194. }
  195. }
  196. return false;
  197. }*/
  198. }
  199. interface IMediaItem
  200. {
  201. /// <summary>
  202. /// Returns the full path of the file or directory
  203. /// </summary>
  204. string Path { get; }
  205. /// <summary>
  206. /// Returns the size of the file or directory to the next largest sector
  207. /// </summary>
  208. Int64 SizeOnDisc { get; }
  209. /// <summary>
  210. /// Returns the Icon of the file or directory
  211. /// </summary>
  212. System.Drawing.Image FileIconImage { get; }
  213. }
  214. /// <summary>
  215. ///
  216. /// </summary>
  217. public class DirectoryItem : IMediaItem
  218. {
  219. private List<IMediaItem> mediaItems = new List<IMediaItem>();
  220. /// <summary>
  221. ///
  222. /// </summary>
  223. /// <param name="directoryPath"></param>
  224. public DirectoryItem(string directoryPath)
  225. {
  226. if (!Directory.Exists(directoryPath))
  227. {
  228. throw new FileNotFoundException("加入DirectoryItem目录未找到!", directoryPath);
  229. }
  230. if (directoryPath.Length <= 3)
  231. {
  232. throw new FileNotFoundException("目录不能直接为驱动盘本身!", directoryPath);
  233. }
  234. m_directoryPath = directoryPath;
  235. FileInfo fileInfo = new FileInfo(m_directoryPath);
  236. displayName = fileInfo.Name;
  237. //
  238. // Get all the files in the directory
  239. //
  240. string[] files = Directory.GetFiles(m_directoryPath);
  241. foreach (string file in files)
  242. {
  243. mediaItems.Add(new FileItem(file));
  244. }
  245. //
  246. // Get all the subdirectories
  247. //
  248. string[] directories = Directory.GetDirectories(m_directoryPath);
  249. foreach (string directory in directories)
  250. {
  251. mediaItems.Add(new DirectoryItem(directory));
  252. }
  253. //
  254. // Get the Directory icon
  255. //
  256. SHFILEINFO shinfo = new SHFILEINFO();
  257. IntPtr hImg = Win32.SHGetFileInfo(m_directoryPath, 0, ref shinfo,
  258. (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
  259. if (shinfo.hIcon != null)
  260. {
  261. //The icon is returned in the hIcon member of the shinfo struct
  262. System.Drawing.IconConverter imageConverter = new System.Drawing.IconConverter();
  263. System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
  264. try
  265. {
  266. fileIconImage = (System.Drawing.Image)
  267. imageConverter.ConvertTo(icon, typeof(System.Drawing.Image));
  268. }
  269. catch (NotSupportedException)
  270. {
  271. }
  272. Win32.DestroyIcon(shinfo.hIcon);
  273. }
  274. }
  275. /// <summary>
  276. ///
  277. /// </summary>
  278. public string Path
  279. {
  280. get
  281. {
  282. return m_directoryPath;
  283. }
  284. }
  285. private string m_directoryPath;
  286. /// <summary>
  287. ///
  288. /// </summary>
  289. public override string ToString()
  290. {
  291. return displayName;
  292. }
  293. private string displayName;
  294. /// <summary>
  295. ///
  296. /// </summary>
  297. public Int64 SizeOnDisc
  298. {
  299. get
  300. {
  301. Int64 totalSize = 0;
  302. foreach (IMediaItem mediaItem in mediaItems)
  303. {
  304. totalSize += mediaItem.SizeOnDisc;
  305. }
  306. return totalSize;
  307. }
  308. }
  309. /// <summary>
  310. ///
  311. /// </summary>
  312. public System.Drawing.Image FileIconImage
  313. {
  314. get
  315. {
  316. return fileIconImage;
  317. }
  318. }
  319. private System.Drawing.Image fileIconImage = null;
  320. }
  321. }