/// <summary> /// constructor /// </summary> /// <param name="directoryPath">the media item path</param> public DirectoryItem(string directoryPath) { try { if (!Directory.Exists(directoryPath)) { throw new FileNotFoundException("The directory added to DirectoryItem was not found!", directoryPath); } _mDirectoryPath = directoryPath; FileInfo fileInfo = new FileInfo(_mDirectoryPath); _displayName = fileInfo.Name; // // Get all the files in the directory // string[] files = Directory.GetFiles(_mDirectoryPath); foreach (string file in files) { _mediaItems.Add(new FileItem(file)); } // // Get all the subdirectories // string[] directories = Directory.GetDirectories(_mDirectoryPath); foreach (string directory in directories) { _mediaItems.Add(new DirectoryItem(directory)); } // // Get the Directory icon // SHFILEINFO shinfo = new SHFILEINFO(); IntPtr hImg = Win32.SHGetFileInfo(_mDirectoryPath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON); if (IntPtr.Zero != shinfo.hIcon) { //The icon is returned in the hIcon member of the shinfo struct System.Drawing.IconConverter imageConverter = new System.Drawing.IconConverter(); System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon); try { _fileIconImage = (System.Drawing.Image) imageConverter.ConvertTo(icon, typeof(System.Drawing.Image)); } catch (NotSupportedException) { } Win32.DestroyIcon(shinfo.hIcon); } } catch (Exception ex) { throw new global::System.InvalidOperationException("Error adding folder" + ex.Message); } }
public FileItem(string path) { try { if (!File.Exists(path)) { throw new FileNotFoundException("The file added to FileItem was not found!", path); } filePath = path; FileInfo fileInfo = new FileInfo(filePath); _displayName = fileInfo.Name; _fileLength = fileInfo.Length; // // Get the File icon // SHFILEINFO shinfo = new SHFILEINFO(); IntPtr hImg = Win32.SHGetFileInfo(filePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON); if (IntPtr.Zero != shinfo.hIcon) { //The icon is returned in the hIcon member of the shinfo struct System.Drawing.IconConverter imageConverter = new System.Drawing.IconConverter(); System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon); try { _fileIconImage = (System.Drawing.Image) imageConverter.ConvertTo(icon, typeof(System.Drawing.Image)); } catch (NotSupportedException) { } Win32.DestroyIcon(shinfo.hIcon); } } catch (Exception ex) { throw new global::System.InvalidOperationException("Error adding file" + ex.Message); } }
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);