Get associated icon from file, folder or drive without WinAPI - c#

Is there a way to get an associated icon in C# without using any DllImports?
When I use Icon.ExtractAssociatedIcon I don't seem to get the icon for drives, folders, SpecialFolder's in Environment or any files on network attached folders. So is there any other way to get an icon using .NET?

.Net have only one method for extracting associated icons, and you already mentioned about it. Every lib you can find uses pinvokes, because they get icons from windows. So if you need to fully portable icon extraction engine, you must write it by yourself, sadly, but this is the only way (and hide all system specific code inside).

Not an answer to your question but might be of use.
You can get most of ms windows icons from this file
$VisualStudiosInstallationDirectory\Common7\VS2008ImageLibrary\VS2008ImageLibrary.zip
Substitute 2008 for 2005 if you still use VS2005

Related

Access a file that was transferred from PC to Android

I want to transfer a file to an Android device using a USB cable and file explorer, and then access it in my Xamarin.Forms app.
As far as I understand there are two ways to do this (and please correct me if I'm wrong):
Since it's an image file - Just put it anywhere and let some
background task find it and add it to what seems to be a virtual
folder of Images. And then access it there.
A more general solution - browse to that specific folder.
Both solutions rely on using FileInfos etc. but unfortunately the folder structure is much different than I see using windows explorer - instead of Alarms, Android, etc. which I see on windows explorer - is see cache, config, etc. using C#, and access to some like data is not allowed (I get an exception). I also tried finding information on how I would search for all images on the device, but either it's not as simple as I thought it would be, or I'm using the wrong keywords.
The file will be put there by a user, so it cannot be added to the project's resources.
Xamarin has a nice way to navigate the files in android, using Android.OS.Environment.ExternalStorageDirectory.Pathwhich you can concatenate with your directory/filename so you can save/access data.
If you want to open the android dialog box so the user can find a file in its device, you can take a look at: https://developer.xamarin.com/recipes/android/data/files/selecting_a_gallery_image/
Remembering that you need to add the READ_EXTERNAL_STORAGE permission to your android manifest file and check runtime permissions. Runtime permissions can be easily checked with https://github.com/jamesmontemagno/PermissionsPlugin

How to change an icon for one single file of the specific type?

I know how to change an icon for a whole file type using registry and file type association.
I also know how to change an icon for a separate folder using desktop.ini file.
My questions is whether it is possible to change an icon for one specific file, let's say one.doc file?
I cannot find a way doing it, yet some upload programs seem to be able to change an icon of the currently uploaded file, while all other files of this type stay with the standard icon. How do they do it?
I can accept a solution in any language; VB, C++, C# - anything goes, though C# is preferable.
Thanks
You can use an IconHandler to allow icons to be customized on a file-by-file basis. Note that shell extensions should not be written in managed code, so C++ is the language of choice here.
Note also that it's highly unusual to be installing an icon handler for another application's file type.
I realize that this thread is pretty old. But for users who have less experience in coding, a good alternative is the following.
Put your target file somewhere.
Create a shortcut item to that file.
Now you can change the icon of the shortcut using its property window.
Just another solution. More of a workaround really.
If you create a SHORTCUT file to that file, then simply RIGHT click on that SHORTCUT, you get the options to change the ICON.

.NET Junction Directories and DirectInfo issues

I am trying to access information under C:\Users\Public\Documents\ which displays as C:\Users\Public\Public Documents\ on explorer. Is there a way to be able to access the DirectoryInfo of a directory under the junction using the pathname displayed in explorer in .NET C#?
When/how are you expecting to get a C:\users\public\public documents path?
If a user goes to copy/paste from Explorer, the C:\users\public\documents path will be copied into the clipboard. Going through a SaveFileDialog also produces the valid path.
If you need a way to access Windows 7 libraries, you can give the Windows API Code Pack a go. You can then access "Public Documents as KnownFolders.PublicDocuments, for example. From what I can see (I've never used it before), there's still no way to reliably turn C:\users\public\public documents into a DirectoryInfo, but you might have better luck digging through the docs.

Pass multiple files / folders from windows explorer to external application

Hi does anyone know how to get windows explorer to pass multiple files / folders through to an external app (c#) referenced in the registry?
I am current able to act upon a single file / folder using the %1 syntax but not sure how to get explorer to pass through multiple items.
Does anyone know how to do this?
When you select multiple files in Explorer, your shell context menu extension's IShellExtInit::Initialize method will be called and pdtobj contains the selection.
Note writing managed shell extension is not supported.
I don't think this is possible.
When you open multiple files using Explorer, it will launch a separate copy of your program for file. I don't think it's possible to override this behavior.
EDIT: I forgot about shell extensions. This is possible.
To work around this, you could make the subsequent copies communicate with the first one, then exit. Detailed instructions for this are beyond the scope of this answer.
In order to do this reliably you would need to write a shell extension, most likely a sendto implementation.
I haven't written one since vb6 but you can find what looks to be a good managed example here
Or you could use a freeware utility

How To Store Files In An EXE

Alright, so I'm working on programming my own installer in C#, and what I'd like to do is something along the lines of put the files in the .exe, so I can do
File.Copy(file, filedir);
Or, if this isn't possible, is there another way of doing what I am attempting to do?
I wouldn't code my own installer, but if you truely want to embed files into your assembly you could use strongly typed resources. In the properties dialog of your project open up the "Resources" tab and then add your file. You'll then be able to get the file using:
ProjectNamespace.Properties.Resources.MyFile
Then you'll be able to write the embedded resource to disk using:
System.IO.File.WriteAllBytes(#"C:\MyFile.bin", ProjectNamespace.Properties.Resources.MyFile);
Honestly, I would suggest you NOT create your own installer. There are many many issues with creating installers. Even the big installer makers don't make their own actual installers anymore, they just create custom MSI packages.
Use Mirosoft Installer (MSI). It's the right thing to do. Make your own custom front-end for it, but don't recreate the already very complex wheel that exists.
UPDATE: If you're just doing this for learning, then I would shy away from thinking of it as "an installer". You might be tempted to take your "research" and use it someday, and frankly, that's how we end up with so many problems when new versions of Windows come out. People create their own wheels with assumptions that aren't valid.
What you're really trying to do is called "packaging", and you really have to become intimately familiar with the Executable PE format, because you're talking about changing the structure of the PE image on disk.
You can simulate it, to a point, with putting files in resources, but that's not really what installers, or self-extractors do.
Here's a link to Self-Extractor tutorial, but it's not in C#.
I don't know enough about the .NET PE requirements to know if you can do this in with a managed code executable or not.
UPDATE2: This is probably more of what you're looking for, it embeds files in the resource, but as I said, it's not really the way professional installers or self-extractors do it. I think there are various limitations on what you can embed as resources. But here's the like to a Self-Extractor Demo written in C#.
I'm guessing here, but if you are trying to store resources in your application before compilation, you can in the Project Explorer, right click a file you would like to add, chose properties and change the type to Embedded Resource.
You can then access the embedded resources later by using the instructions from this KB:
http://support.microsoft.com/kb/319292
in case you simply want to store multiple files in a single file storage (and extract files from there, interact etc.) you might also want to check out NFileStorage, a .net file storage. written in 100% .NET C# with all sources included. It also comes with a command line interpreter that allows interaction from the command line.

Categories

Resources