public static RunCommand CreateCommandFromstring ( string path ) { RunCommand returnCommand = null; try { FileInfo file = new FileInfo ( path ); string tempPath = System. IO. Path. GetTempPath ( ) + System. IO. Path. GetFileNameWithoutExtension ( path ) + ".png"; string quickyName = System. IO. Path. GetFileNameWithoutExtension ( path ); //Before adding check if name exists int counter = 0; string dummyName = quickyName; //make sure the name is unique! if ( RepositoryLibrary. ExistsQuicky ( quickyName ) ) { do { quickyName = dummyName + "(" + counter++. ToString ( ) + ")"; } while ( RepositoryLibrary. ExistsQuicky ( quickyName ) ); } FileIcon fi = null; try { fi = new FileIcon ( path ); labelXx2: try { File. Delete ( tempPath ); //clean file if it already exists } catch ( Exception ) { //File is prob in use.. increment file name with datetimenow tempPath = System. IO. Path. GetTempPath ( ) + System. IO. Path. GetFileNameWithoutExtension ( path ) + DateTime. Now. Millisecond. ToString ( ) + ".png"; goto labelXx2; } //get icon and save it to temp location fi. ShellIcon. ToBitmap ( ). Save ( tempPath, System. Drawing. Imaging. ImageFormat. Png ); } catch ( Exception ) { //if it did not succeed do nothing, this is only for icon so null it tempPath = ""; } finally { fi. ShellIcon. Dispose ( ); } returnCommand = new RunCommand { IconLocation = tempPath, Command = path, Quicky = quickyName, Arguments = string. Empty, Desctiption = string. Empty }; return returnCommand; } catch ( Exception ) { return returnCommand; throw; } finally { } }
/// <summary> /// Constructs a new instance of the FileIcon class /// and retrieves the information specified in the /// flags. /// </summary> /// <param name="fileName">The filename to get information /// for</param> /// <param name="flags">The flags to use when extracting the /// icon and other shell information.</param> public FileIcon(string fileName, FileIcon.SHGetFileInfoConstants flags) { this.fileName = fileName; this.flags = flags; GetInfo(); }
private Icon getIcon(bool large) { // Get icon index and path: int iconIndex = 0; StringBuilder iconPath = new StringBuilder(260, 260); if (linkA == null) { linkW.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex); } else { linkA.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex); } string iconFile = iconPath.ToString(); // If there are no details set for the icon, then we must use // the shell to get the icon for the target: if (iconFile.Length == 0) { // Use the FileIcon object to get the icon: FileIcon.SHGetFileInfoConstants flags = FileIcon.SHGetFileInfoConstants.SHGFI_ICON | FileIcon.SHGetFileInfoConstants.SHGFI_ATTRIBUTES; if (large) { flags = flags | FileIcon.SHGetFileInfoConstants.SHGFI_LARGEICON; } else { flags = flags | FileIcon.SHGetFileInfoConstants.SHGFI_SMALLICON; } FileIcon fileIcon = new FileIcon(Target, flags); return fileIcon.ShellIcon; } else { // Use ExtractIconEx to get the icon: IntPtr[] hIconEx = new IntPtr[1] {IntPtr.Zero}; int iconCount = 0; if (large) { iconCount = UnManagedMethods.ExtractIconEx( iconFile, iconIndex, hIconEx, null, 1); } else { iconCount = UnManagedMethods.ExtractIconEx( iconFile, iconIndex, null, hIconEx, 1); } // If success then return as a GDI+ object Icon icon = null; if (hIconEx[0] != IntPtr.Zero) { icon = Icon.FromHandle(hIconEx[0]); //UnManagedMethods.DestroyIcon(hIconEx[0]); } return icon; } }