How to programmatically create a file association that works in XP, Vista and Windows 7

北城余情 提交于 2019-11-30 15:37:17

问题


I want to be able to register my file-extension from my program. (Associate the filetype with my program)

There are lots of articles about this, but the technique discussed (fiddling with the registry) is only appropriate for Windows XP as far as I see.

What is the best way to associate a filetype with a program so that it works under XP, Vista and Windows 7 as well ? (Are there any working code-examples ?)

ClickOnce would be an option, but this only registers the filetype at installation-time while I want to be able to register filetypes at runtime.


回答1:


In Windows Vista (and presumably Windows 7) file associations have changed: if a file is already associated with a program and you reassociate it, doubleclicking the file won't open it with the new program. Instead, the association is added to the list under "open with". Also see this little discussion on registering your application for associations in Vista (Web Archive, original link is gone).

I'm not sure whether it is good to change this. However, this post discusses the various keys in the registry that you can use. But even better, this codeproject article gives you ready to use code for associating a file at runtime. I don't know whether it works any differently on Vista+ though.




回答2:


If you need to add file association in a non admin scenario, you can use User-specific file associations which is described in this article. Check out the "User-specific file associations".




回答3:


There were only a couple changes from XP to Vista/7 as far as file association goes, so most examples for XP should still work. The only think you should be concerned about is the 'Default Programs' setting in Vista/7 that did't exist in XP and overrides other file association registration.



来源:https://stackoverflow.com/questions/1807742/how-to-programmatically-create-a-file-association-that-works-in-xp-vista-and-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!