public static int GetIndexForFile(string file) { string key; // icon files and exe files can have their custom icons if (Path.GetExtension(file).Equals(".ico", StringComparison.OrdinalIgnoreCase) || Path.GetExtension(file).Equals(".exe", StringComparison.OrdinalIgnoreCase)) { key = file; } else { key = Path.GetExtension(file).ToLower(); } // clear the icon cache if (icons.Images.Count > 100) { icons.Images.Clear(); iconIndecies.Clear(); } if (iconIndecies.Contains(key)) { return((int)iconIndecies[key]); } else { icons.Images.Add(DriveObject.GetImageForFile(file)); int index = icons.Images.Count - 1; iconIndecies.Add(key, index); return(index); } }
public ShellTree() { Sorted = true; TreeNode rootNode = Nodes.Add("Desktop"); rootNode.ImageIndex = 6; rootNode.SelectedImageIndex = 6; rootNode.Tag = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); TreeNode myFilesNode = rootNode.Nodes.Add("My Documents"); myFilesNode.ImageIndex = 7; myFilesNode.SelectedImageIndex = 7; try { myFilesNode.Tag = Environment.GetFolderPath(Environment.SpecialFolder.Personal); } catch (Exception) { myFilesNode.Tag = "C:\\"; } myFilesNode.Nodes.Add(""); TreeNode computerNode = rootNode.Nodes.Add("My Computer"); computerNode.ImageIndex = 8; computerNode.SelectedImageIndex = 8; try { computerNode.Tag = Environment.GetFolderPath(Environment.SpecialFolder.Personal); } catch (Exception) { computerNode.Tag = "C:\\"; } foreach (string driveName in Environment.GetLogicalDrives()) { DriveObject drive = new DriveObject(driveName); TreeNode node = new TreeNode(drive.ToString()); node.Nodes.Add(new TreeNode("")); node.Tag = driveName.Substring(0, driveName.Length - 1); computerNode.Nodes.Add(node); switch (DriveObject.GetDriveType(driveName)) { case DriveType.Removeable: node.ImageIndex = node.SelectedImageIndex = 2; break; case DriveType.Fixed: node.ImageIndex = node.SelectedImageIndex = 3; break; case DriveType.Cdrom: node.ImageIndex = node.SelectedImageIndex = 4; break; case DriveType.Remote: node.ImageIndex = node.SelectedImageIndex = 5; break; default: node.ImageIndex = node.SelectedImageIndex = 3; break; } } foreach (string directory in Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory))) { TreeNode node = rootNode.Nodes.Add(Path.GetFileName(directory)); node.Tag = directory; node.ImageIndex = node.SelectedImageIndex = 0; node.Nodes.Add(new TreeNode("")); } rootNode.Expand(); computerNode.Expand(); InitializeComponent(); }