我正在编写自己的基于 C# 的应用程序启动器,虽然我让它在其中填充TreeView
和启动应用程序快捷方式,但我似乎无法弄清楚如何将图标作为图像添加到TreeView
. 我当前获取文件的代码是:
private void homeMenu_Load(object sender, EventArgs e)
{
this.ShowInTaskbar = false;
if (Directory.Exists((Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName + "\\Roaming\\Launcher")))
{
}
else
{
Directory.CreateDirectory(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName + "\\Roaming\\Launcher");
}
DirectoryInfo launcherFiles = new DirectoryInfo(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName + "\\Roaming\\Launcher");
lstPrograms.Nodes.Add(CreatingDirectoryTreeNode(launcherFiles));
lstPrograms.Sort();
}
private static TreeNode CreatingDirectoryTreeNode(DirectoryInfo directoryInfo)
{
var directoryNode = new TreeNode(directoryInfo.Name);
foreach (var directory in directoryInfo.GetDirectories())
{
directoryNode.Nodes.Add(CreatingDirectoryTreeNode(directory));
}
foreach (var file in directoryInfo.GetFiles())
{
directoryNode.Nodes.Add(new TreeNode(file.Name));
}
return directoryNode;
}
我遇到的主要问题是将 TreeList 的 ImageList 的图标添加到特定节点。我知道我需要添加:
lstPrograms.ImageList.Images.Add(Icon.ExtractAssociatedIcon());
要将图标实际添加到图像列表中,如何获取该特定图像的索引,然后将其TreeView
与其相关文件一起添加到?