How to get the system icon for any file/folder - c#

How can I retrieve the system icon associated with a file/folder so that
I can show it in the list view adjacent to the file/folder name?

You need to use Icon.ExtractAssociatedIcon
Icon icon = Icon.ExtractAssociatedIcon(filepath);
Take a look at Icon.ExtractAssociatedIcon documentation
Note: this works only for files. For folders you need P/Invoke sample is here Edit: Page is now defunct, please refer to this copy on the Wayback Machine.

See Obtaining (and managing) file and folder icons using SHGetFileInfo in C#.

Related

How does Windows Photo Viewer get the files for the slideshow from Windows Explorer?

I wrote an image viewer application in C# to replace Windows 7 Photo Viewer which does not have the one feature I needed, which was to set the ratings and keywords directly from the view screen and not by opening some less user-friendly property pages. It works Ok for my need, but I wanted to improve it with a slideshow.
With Windows 7 Photo Viewer, what I usually did was I set the keywords of the picture, I used Windows Explorer "organize by" feature, clicked one keyword, double-clicked one file and run the slideshow from there.
But when I double-click a file from the keyword-"organized" folder in Windows Explorer, all my app gets is the command-line argument, i.e. the full name of the file.
I could admit running my slideshow from there, using System.IO.Path.GetDirectoryName to get all files from the folder, but I find it lacks a certain "panache", don't you think?
I am not asking for code here. But could someone just point me in the right direction, please? I don't know what WindowsExplorer actually gives PhotoViewer that allows it to only show the required files. And why.
Thanks for any help.
When you use Organize by feature of Windows Explorer it creates special virtual folder. And this folder contains files with selected keyword only. Mechanism of opening of PhotoViewer differs from standard way. If you open HKEY_CLASSES_ROOT\jpegfile\shell\open registry key you will see that where is DropTarget subkey. It means when you double-clicked the file shell creates inproc com server with CLSID from DropTarget subkey and pass virtual path of image to IDropTarget instance. So PhotoViewer work with list of virtual objects instead of physical directory.

Load image from folder in solution?

I'm trying to load an image from a folder in the solution, but I only get a error message that it's not found. What have I done wrong? The code below is in the MainForm.cs that is at the same level as the Resource folder. Help is preciated! Thanks!
// Images
Image imageCircle = Image.FromFile("Resources/circle.png");
// Set deafult picture on start
pictureBox1.Image = imageCircle;
Edit: Fixed Broken Links
Take a look at this MSDN article, it discusses Adding and Editing Resources and what your options are, and this MSDN article discussing Linked and Embedded resources using the Resource Designer.
Then select your file
Then you can access it like Madurika suggests.
i.e.
Image imageCircle = YourPojectName.Properties.Resources.YourFileNameHere;
It always take the path from the where executable is located(bin folder). So if you can access it using full path, problem will solved. Or you can have a configuration item for the root folder. then access like Image.FromFile(rootFolder+ "Resources/circle.png");. Anyway this issue wont be there when you deploy it.
And if you are using resource file,
<projectName>.Properties.Resources.<ImageName>;
will return the image.
The program is executed in bin/debug (the place where all .dll's are), put the Resources folder there.

silverlight - file extensions

I am working on a file explorer using Silverlight OOB. I need a way to get and display the icon associated with each file in my application. Note, I only need to show the icons, I donĀ“t need to open the files.
If I understood you right, you're building something like Windows Explorer and want to mimic its list view by showing the program icons right before their names.
I'm not sure whether OOB has access to the System.Drawing.Icon class, but if so, you can use the following code to get the icon for any given file:
Bitmap icon = System.Drawing.Icon.ExtractAssociatedIcon(filename).ToBitmap();
If not, the only way you can do it is by storing the icons for most common file formats in a dictionary and retrieving them from there based upon file extension.

Access to system imagelist

I am creating a treeview which wil have folder images for directories and want to know if there is a way to access the system image list so that I can display the system default folder image.
Thx
Mark.
There is no out-of-the-box image list for the system images (they depend on things like the current visual theme). The entry for SHGetFileInfo at pinvoke.net has a code sample for extracting icons for files (and, I presume, folders). You can use that and dynamically populate an ImageList component to get the tree view display the appropriate icons.
To get the current system icons you should take a look into the registry at HKEY_CLASSES_ROOT\Folder\DefaultIcon. Here you'll find the path and position of the used folder icon.

List all topics from a CHM file

I am trying to get a CHM file to open to a specific topic using C#.
I have tried using
Help.ShowHelp(this, path, HelpNavigator.Topic, "TopicTitle");
but it doesn't find the page. I must not be keying in the topic title correctly. Is there a way that I can programatically retrieve all of the topics from a CHM file so that I can see what they are?
No, the HtmlHelp API function is far too primitive to support enumerating topics. You could use the 7-zip file manager to look inside the .chm file. Right-click the file and choose "Open Inside". Or use the help authoring tool that was used.
If you open a CHM file, and right click on a help page, you can choose the Properties command.
In the middle of the Properties page there is a property called: Address (URL).
The end of the URL contains the topic string used to open the help file to the correct page.
Here is an example:
mk:#MSITStore:C:\Program%20Files\Sisulizer%202010\Sisulizer.chm::/OutputFiles.htm
If the URL is too long to see the topic at the end, you can select the address with your mouse and scroll to the end.
Here is a screen shot.
You may also use the following, where path is path to the chm file:
using System.Windows.Forms;
Help.ShowHelp(this, path, HelpNavigator.KeywordIndex, "Topic title");
I am not sure about how to programmatically retrieve topics from CHM.
But I changed the one line code this way and it worked.
Help.ShowHelp(this, path , HelpNavigator.TopicId,"TopicTitle");

Categories