|
|
Comments and Discussions
|
|
 |

|
I am trying this on windows 7 machine.
I only want to find out the default icon associated with an arbitrary file like for instance pdf. For pdf, the path in programicon is null and for txt extensions I get a value back containing % charackters that obviously need to be replaced by valid directory names.
So I think there is still some work here.
Can't figure out why pdf's return an empty path though.
|
|
|
|

|
Hi,
How to replace an existing extension with another program as default, BUT keep the old one in the "open with" list ?
Thanks!
|
|
|
|

|
When I select more than one file, my application open n times for each file selected with a single environment argument.
|
|
|
|

|
For those who are interested I created a NuGet package for this library to make it easier to pull into newer projects
The code for creating the NuGet package is on BitBucket and you can find the package on NuGet
|
|
|
|

|
Doesn't work with windows 7, it creates registry entries in HKEY_CLASSES_ROOT correctly but this is not where they should be placed, they must go under HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\EXPLORER\FILEEXTS\-name of your extension-
If one manually adds the appropriate entries under the above mentioned key, the association works well, and if one deletes it, its stops working, so I conclude that on my system this is the reg. key that has to be modified.
If I delete one of the other places with relevant entries, it has no effect.
Other locations that I have found (with the relevant entries) are:
A) HKEY_LOCAL_MACHINE\SOFTWARE\CLASSES\name-of-extension\OPENWITHLIST
B) HKEY_CLASSES_ROOT\-name of extension-
C) HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\EXPLORER\FILEEXTS\-name of your extension-
D) HKEY_USERS\random-user-ID\SOFTWARE\MICROSOFT\WINDOWS
ONLY by changing C) the changes have any effect!
|
|
|
|

|
Is '%1' the path of the opened file?
And if that is then can I get the path with this code?:
static void Main(string[] args)
{
string FilePath = args[0];
}
|
|
|
|
|
|

|
This is one of the very better articles that I have come across in a considerable period of time. A job well done.
|
|
|
|

|
in file ProgramAssociationInfo.cs the line 479 needs to be changed to:
registryWrapper.Write(string.Format("{0}\\{1}", this.progId, "DefaultIcon"), null, icon.ToString());
This fixes the issue I had with me not able to change the DefaultIcon
|
|
|
|
|

|
It should be noted that the HKEY_CLASSES_ROOT key is actually a conglomerate view of the HKEY_LOCAL_MACHINE\Software\Classes AND the HKEY_CURRENT_USER\Software\Class, so please be careful, as sometimes one will override the other.
Also, when windows first launches a file association it creates a key under:
HKEY_CLASSES_ROOT\Software\Classes\Applications\{APPFILENAME}, where {APPFILENAME} is the filename including extension of the program associated, under this it will create a copy of the keys under the actual file association, this becomes the default when using the windows interface to create an association, which becomes a problem is the file pointed to under the Applications key no longer exists, as it stops the file from appearing in the open with list (even if you use the browse button), this key is not removed by the windows XP (maybe vista and 7 not sure) interface when going through the file types settings and restoring or deleting the association.
So if you have a problem where an association is not working be sure to check for this key and remove it.
One other place file assocations can be created is the Recommended Association list, this list can be found under:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts
And lists the default recommended programs, as per usual microsoft makes everything more complication then it needs to be, I hope this message saves someone some time, i had to find this by trial and error in the registry because it dosnt appear to be mentioned anywhere else on the net.
|
|
|
|

|
Dear Mr. Grant,
I hope you get out of debt fast!
This is a great project. I was looking through it and found one little oopsie that you may want to know about, although I realize that you probably haven't touched this thing in a while. In AssociationGUIForm.cs, in the extensionsListBox_SelectedIndexChanged(object, EventArgs) around line 33 of that function, you have what seems like an identical if-then statement doing the same thing, but the second time around, also setting the tag for the textbox. It looks like you wanted to replace the first if-then, but instead, replicated it and added to it. If I see more, I'll let you know, but this is a great job. Nice Registry Wrapper!
In Christ,
Aaron Laws
http://ProCure.com
|
|
|
|
|

|
Hi,
I was wondering if someone could give us a hand here. I've created my own classes which sets file assiciations in the registry, much similar to this project... it seems to work perfectly fine if I register my own custom file type, but if I want to change an existing extension (like .tif for instnace) to open in my app instead of the default or current app, it won't work until I go into the associated app's settings and (un)associate the file type.
I've scanned the registry to check what it's doing, even by making use of these classes, and it sets the verbs, paths and parameters as it should, but windows still opens it in the original program (unless I first go manually unassociate)
I've been stuck on this for 5 days... my project was supposed to have been live already... it's freaking my pip. Any ideas would be awesome.
Tks,
Pip
|
|
|
|

|
Dear Pip,
Wow, that sucks. How did you do? I have no idea what's going on, but am curious as to how you did.
In Christ,
Aaron Laws
http://ProCure.com
|
|
|
|

|
You most likely have the applications key left over, to reset a file association FULLY using the registry delete these keys (NOTE: the windows interface dosnt even reset some of these):
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.{EXT}
HKEY_LOCAL_MACHINE\Software\Classes\.{EXT}]
HKEY_LOCAL_MACHINE\Software\Classes\Applications\{YOURAPPNAME}
HKEY_LOCAL_MACHINE\Software\Classes\{EXT}_auto_file
HKEY_CURRENT_USER\Software\Classes\.{EXT}]
HKEY_CURRENT_USER\Software\Classes\Applications\{YOURAPPNAME}
HKEY_CURRENT_USER\Software\Classes\{EXT}_auto_file]
Replacing
{EXT} with your file extension
{YOURAPPNAME} with the FILENAME.EXT of your application
Doing this will not only ensure your newly created assocation will work, but it will also remove any crap that windows has added from the open with interface creating its associations ({EXT}_auto_file and the like).
|
|
|
|

|
I am developing open - source free project under GNU public license. I want to use your code as is, without modifying, just add your project to my solution. Can I use your code ?
I see somewhere in a code, that it can't be used in a GNU projects.
|
|
|
|

|
I am not able to associate your files with my project kindly let me knw urgently.....
|
|
|
|

|
Nice works, but I don't know how to step by step use it in my application. All I want is that after install my application, user can open a file by double click into it, of course this file with my extension. Can you describe more details?
Michael Egan
|
|
|
|

|
Hi,
It does not work under VISTA, I don t know why !
Do you have an idea ?
|
|
|
|

|
A little tweak for proper file handling if filename is with spaces like "C:\Documents and settings\...", shoud be:
//Path and arguments to use
@"C:\SomePath\MyApp.exe ""%1"""
|
|
|
|

|
Brendan,
How to get the icon from path string of ProgramAssociationInfo.DefaultIcon ?
I'm writing a explorer type program, and will need to display icon for each file type.
You done a great job, , thanks much.
Anh Tuan
|
|
|
|

|
mmhh... You can get the path to the icon and load it into your ListView (Or any other control) using standart C# Commands!
FileAssociationInfo hFAInfo = new FileAssociationInfo(".test");
if (hFAInfo.Exists)
{
ProgramAssociationInfo hPAInfo = new ProgramAssociationInfo(hFAInfo.ProgID);
string sIconPath = hPAInfo.DefaultIcon.Path.ToString();
}
Greetings!
Bier
|
|
|
|

|
Hey!
Nice Code... pretty easy to use! But I found an Bug... If I set an DefaultIcon for a ProgrammAssociation the libary creates an DefaultIcon string value to my icon. At least Windows (XP... dunno how it is in other Win Versions) uses a key with an string (Standart) including the Icon. Your GUI even doesn't show my icon!
To make this a little bit more understood I try to show this on an "example":
Your's libary creates something like this:
-[KEY]Prog.ID
--(Standard)
--DefaultIcon
--[KEY]shell
...
But Windows wants something like this:
-[KEY]Prog.ID
--(Standard)
--[KEY]DefaultIcon
---(Standard) //Path to the Icon
--[KEY]shell
...
I try to fix this, but it would be cool if you could try this too!
Greetings and thanks for the nice code! =)
//EDIT:
protected void SetDefaultIcon(ProgramIcon icon)
{
if (!this.Exists)
throw new Exception("Extension does not exist");
if (icon != ProgramIcon.None)
{
RegistryKey root = Registry.ClassesRoot;
RegistryKey key = root.OpenSubKey(this.progId, true);
RegistryKey tmpkey = key.OpenSubKey("DefaultIcon", true);
if (tmpkey != null)
{
key.DeleteSubKeyTree("shell");
}
tmpkey = key.CreateSubKey("DefaultIcon");
registryWrapper.Write(this.progId + "\\DefaultIcon", "", icon.ToString());
ShellNotification.NotifyOfChange();
}
}
Works for me
Greetz!
-- modified at 5:43 Sunday 2nd December, 2007
Bier
|
|
|
|

|
This code is incorrect. Correct version:
protected void SetDefaultIcon(ProgramIcon icon)
{
if (!this.Exists)
throw new Exception("Extension does not exist");
if (icon != ProgramIcon.None)
{
RegistryKey root = Registry.ClassesRoot;
RegistryKey key = root.OpenSubKey(this.progId, true);
RegistryKey tmpkey = key.OpenSubKey("DefaultIcon", true);
if (tmpkey == null)
{
tmpkey = key.CreateSubKey("DefaultIcon");
}
registryWrapper.Write(this.progId + "\\DefaultIcon", "", icon.ToString());
ShellNotification.NotifyOfChange();
}
}
|
|
|
|

|
I tried compiling your code but I'm facing an exception "Access to registry key HKEY_...ROOT\.ewf is denied"
I'm running Vista Busines.
"Do u think that my being stronger or faster has anything to do with my muscles in a place like this".
|
|
|
|

|
Are you receiving that exception during compilation or execution?
Chances are you are running into that exception because your user account doesn't have permission to write to or create that registry key.
Currently my code only writes to HKEY_CLASSES_ROOT, perhaps in future it will support writing to HKEY_CURRENT_USER\Software\Classes as well so that an app can register user specific file associations.
Ordinarily when writing to an admin only portion of the registry, Vista's Registry Virtualization would kick in... however it doesn't apply in this case as we are writing to HKEY_CLASSES_ROOT and not HKEY_LOCAL_MACHINE\Software.
For now, try running the application as Administrator when setting/editing system wide associations (which is truly an admin operation).
|
|
|
|

|
In vista, the file associations information is written in HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts instead of HKEY_CLASSES_ROOT.
It will read HKEY_CLASSES_ROOT unless there is no infomation existed in HKEY_CURRENT_USER.
And the key in HKEY_CURRENT_USER will be created when user opens file associations in the contro panel.
|
|
|
|

|
Vista (and it seems, also XP) support per-user file associations, at least as far as assigning extensions to existing file types. HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\ is where it's at... I don't see actual file types under there, just associations for specific extensions.
It's also possible that HKCR is one of the keys where any writes by limited users is mirrored into a per-user key, and read back when requested by that user.
At the very least, the stuff in the key I mentioned has the possibility of overriding what changes your code tries to make if it doesn't look there.
|
|
|
|

|
Hi!
These classes you wrote are exactly what we need. I was wondering if we can use it in our program?
Thanks so much!
Richard
|
|
|
|

|
Absolutely!
When you get around to release I’d love to hear what you are using it for/in.
|
|
|
|

|
Hi, this code is not as flexible as the version provided by Brendan but covers the common case of registering an application for opening a file with a specific extension. Matthias /// <summary> /// Registers a file type via it's extension. If the file type is already registered, nothing is changed. /// </summary> /// <param name="extension">The extension to register</param> /// <param name="progId">A unique identifier for the program to work with the file type</param> /// <param name="description">A brief description of the file type</param> /// <param name="executeable">Where to find the executeable.</param> /// <param name="iconFile">Location of the icon.</param> /// <param name="iconIdx">Selects the icon within <paramref name="iconFile"/></param> public static void Register(string extension, string progId, string description, string executeable, string iconFile, int iconIdx) { try { if (extension.Length != 0) { if (extension[0] != '.') { extension = "."+extension; } // register the extension, if necessary using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(extension)) { if (key == null) { using (RegistryKey extKey = Registry.ClassesRoot.CreateSubKey(extension)) { extKey.SetValue(string.Empty, progId); } } } // register the progId, if necessary using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(progId)) { if (key == null) { using (RegistryKey progIdKey = Registry.ClassesRoot.CreateSubKey(progId)) { progIdKey.SetValue(string.Empty, description); using (RegistryKey defaultIcon = progIdKey.CreateSubKey("DefaultIcon")) { defaultIcon.SetValue(string.Empty, String.Format("\"{0}\",{1}", iconFile, iconIdx)); } using (RegistryKey command = progIdKey.CreateSubKey("shell\\open\\command")) { command.SetValue(string.Empty, String.Format("\"{0}\" \"%1\"", executeable)); } } } } } } catch(Exception ex) { // TODO: implement proper exception handling. } }
-- modified at 2:08 Wednesday 30th May, 2007
|
|
|
|

|
Nice, but don't work unter Vista.
|
|
|
|

|
Sure it works on vista !
You're german , aren't you ? Because you used the german "unter" instead of "under"
|
|
|
|

|
Much easier.
And, unlike the code from the main article, it actually works
Cheers!
|
|
|
|

|
This code didn't work for me at first. But then after correction, it works great.
The trouble is that it only works if there is not already any association with a file.
However, if you want to "overwrite" an existing association with your own, it fails.
It's easy to fix the logic, just look how it calls OpenSubKey() and then skips setting
the value if it already exists.
Instead, modify the code to always set the values even if OpenSubKey() finds an association
already there. Alternatively, you could warn the user, if you like, before overwriting.
Sincerely,
Wayne
|
|
|
|

|
Brendan,
Where's the VB version for us
amatuers that don't want to learn/ install C# ?
See ya on the forums!
|
|
|
|

|
Curse you Tall Dude! I was hoping it’d be a week or two before someone asked about a VB.NET version but then you had to go and ruin that! Ahh well, it was expected.
I’m planning to sit down over the weekend and convert both projects to VB.NET as well as have a pre-compiled copy of the classes for those who don’t want to use either C# or VB.NET and/or don’t want to look at source code at all.
|
|
|
|

|
Well God bless you Brendan.
And yes, I've read your religious position
on your blog
Good luck on your weight project. I am also
6'4", but a smoker and could use to ADD a few pounds.
But on the bright side, I'll never be as good a programmer
as you are.
Later my friend .....
|
|
|
|

|
Wanna do a little beta testing for me?
At long last I got around to porting the projects over to VB.NET and would love to have you (or anyone else) take it for a spin before I post it as the final VB.NET version on here.
You can find the project here[^]
|
|
|
|

|
Brendan,
I had a little while today to try out the VB example solution.
Problem 1, (for me), VB Express will not open and run the program
in debug mode because it does not use the VB express partial classes
setup. Also, I, being an amatuer, was not used to working with multiple
projects in one solution
It would run by directly clicking on the exe in the solution from
Windows Explorer.
(I did restructure the code to run in the VB Express debugger.)
Problem 2: At one point I was able to crash the program by using the top
'update' button with the 'persistant handler' set to blank.
Problem 3: Having not read up too much on the subject of how Windows
handles file associations, the program was not intuative to me.
(Me dumb)
When trying to use the 'new extension' option, I had no idea what to put in the
other fields. Wheather thay could be made up, or had to equal some real value.
Also, the 'persistanthandler' seems to be a GUID, which I know nothing about.
Where would I look to find the GUID for my VB program?
A lot of the 'program association' info at the bottom, ended in
'/n /dde', which I have no idea what that means.
I figured out for my program to gets it's file argument I had to use "%1"
at the end, which did work.
The 'add' and 'remove' at the bottom, let you add/remove things like 'open'
(called a verb?) The open at bottom is the only one I worked with.
Problem 4: Some of the updates would not show up until the program was
closed and re-opened. Can't remember which ones.
Problem 5: Pointing to a new exe (VB program exe) for the icon source did not
change the icon source. (Is that just an info only field?)
More comments when I get more time to tinker.
|
|
|
|

|
This sure is helpful! Now we don't need ourselves to dive into the registry again
|
|
|
|

|
Thanks! The joy of registry editing was exactly why I wrote this in fact. I had found oodles of examples of how to write to the registry to do it yourself... but almost nothing that provided a higher level interface to do it.
|
|
|
|

|
I have been having a mare with file associations. I couldn't figure out how to get it to work with the installer, and then when I used XP's own file association tools from explorer, it just kept ignoring what I just told it.
I wanted to get this behaviour...
When you click on a *.spr file launch my program, then send the file as a command line switch. But I couldn't get it to work.
I got the associate.exe from the resource kit and assigned it manually, and it works, but I'm still confused.
|
|
|
|

|
Getting files associations to work depends on what route you are taking to create them. If you are having problems with the installer I’d look more carefully into what might be going wrong there. What sort of installer are you using? How are you creating it?
|
|
|
|

|
Well I was using a 3rd party installer (wise) with .net, but we don't have the latest version, and I don't think we could have version 7, so I tried to use the vs2005 version, which I can't figure out how to make associations.
But I do like programs that are well behaved and can fix things themselves.
|
|
|
|

|
Assuming you are trying to set it up within a Visual Studio 2005 Setup Project, use the File Types Editor (should be middle icon in Solution Explorer) to specify new file types you want registered on the target system and how they should be treated.
|
|
|
|

|
Most often when you are having problems with changing file associations in windows, be it both XP and Vista, they can get angry about program IDs if the old program isn't where it expects it, it ignores changes... if you are having the trouble, just delete the entire key for that extension. and recreate it. It works wonders.
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
This article describes the use of several custom C# classes that can be used to create, view, edit and delete Windows file associations.
| Type | Article |
| Licence | |
| First Posted | 5 Jan 2007 |
| Views | 150,917 |
| Downloads | 4,906 |
| Bookmarked | 128 times |
|
|