Question How to change icon for custom filetype

azzdawg_dev

New member
Joined
Apr 30, 2012
Messages
4
Programming Experience
1-3
hi there,

i'm trying to write a program that sets a file association and sets the icon for that filetype. so far, i have accomplished the filetype association part, and loading the data from the file into my app.

the problem is that i cannot figure out how to set the icon.

i have tried this:
VB.NET:
Dim rk As Microsoft.Win32.RegistryKey = ClassesRoot		Dim rk1 As Microsoft.Win32.RegistryKey = ClassesRoot
		Dim ext As Microsoft.Win32.RegistryKey = rk.OpenSubKey(".tst")
		Dim regtype As String = ext.GetValue(String.Empty)
		ext = rk1.OpenSubKey(regtype, True).OpenSubKey("DefaultIcon", True)
		ext.SetValue(String.Empty, "D:\dev\pen.ico")

i have also tried writing a much simpler piece of code that eliminates the unneccesary declarations above. i have scoured the net, nothing is working (also, everything i've found supposedly works up to vista. not windows 7 and later)

please do not post other languages (even C#). if you believe you have the answer, but its a different language, translate it into vb.net (if you use a code converter, test it BEFORE posting!)

thank you in advance for your help!
 
If you want your app to be the default for a file type then that is something that you should be doing with your installer, not in code. Both ClickOnce publishing and Windows Installer Setup projects support that and pretty much any third-party installer will too.
 
not the answer

If you want your app to be the default for a file type then that is something that you should be doing with your installer, not in code. Both ClickOnce publishing and Windows Installer Setup projects support that and pretty much any third-party installer will too.

that does not answer my question. read the top post properly. i have accomplished setting app as default for that file type. using code is preffered, so the user can change associations as they wish. installer means to reassociate, they must reinstall the program. please post only if you have the answer.

i need to know how to set the icon for that file using code. i don't want to know how to associate the file so double click opens it with my app, that is already done
 
that does not answer my question. read the top post properly.
I did read it properly. You can create the association with an installer and you can edit that association in Windows so what you're doing is basically pointless. Of course, that's your prerogative. It's not for you to say what other people can and cannot post. If I think that you can benefit from some information that I have then I will provide it, whether it answers the question you asked or not. I lost count several years ago of the number of people who posted one question while the solution to their problem was something else entirely. If you choose not to use the information I provide then that is also your prerogative, but it's my prerogative whether to provide it or not.
 
I did read it properly. You can create the association with an installer and you can edit that association in Windows so what you're doing is basically pointless. Of course, that's your prerogative. It's not for you to say what other people can and cannot post. If I think that you can benefit from some information that I have then I will provide it, whether it answers the question you asked or not. I lost count several years ago of the number of people who posted one question while the solution to their problem was something else entirely. If you choose not to use the information I provide then that is also your prerogative, but it's my prerogative whether to provide it or not.


unlike those people, i actually know what i'm doing. i've been doing this for years. i need to do EXACTLY this: set an icon for a specific filetype (so that icon is what displays on that filetype in windows explorer). i need to do it with code. one reason for doing it with code is that (and its happened with me a few times) if a user downloads a "better" program (lets say photoshop), but it uses too much memory and crashes all the time (or something else like the trial expires), or they simply don't like it, they may want to revert back. short of reinstalling, they just change the settings in the program and one click, that filetype reassociates itself with my app (and sets the icon back to my apps default icon for that filetype).

please understand that i have made a request, and that i know exactly what i ACTUALLY requested

the short version of the question is "i need to use code to change the icon for a specific filetype (lets say .myfiletype)". to eliminate one posibility as to why i'm having trouble, i'm not making an idiotic error and using a picture as an icon. i'm using an icon (.ico) file as an icon. I can say this though: this is the first time i have ever made a program that needs to associate filetypes (solved) and set an icon for a certain filetype (not yet solved).
 
unlike those people, i actually know what i'm doing.
I guess I missed where you said that in your first post. I guess that's what I didn't read properly. If what I posted doesn't help then by all means ignore it but if I made an incorrect assumption then so did you. Anyway, I'll leave you to those who may give you what you want no questions asked.
 
thanks it worked!

for those who don't understand the SHCHANGENOTIFY part, it just refreshes the shell, displaying the icon

you have to declare two constants:

VB.NET:
Private Const SHCNE_ASSOCCHANGED As Long = &H8000000
Private Const SHCNF_IDLIST As Long = &H0

and call it like this:
VB.NET:
'you need an error handler (working on that part)
Call SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, Nothing, Nothing)


you will need an error handler, because when i tested it, it returned an error, resulting in the program to crash, but it still worked.

PS: when i said unlike others, i know what i'm saying; i don't mean it to be offensive. it just means that i'm past the beginner stage (well and truly) and i would rate myself as quite advanced
 
Back
Top