views:

5139

answers:

5

Hi,

I have a few questions related:

1) Is possible to make my program change filetype association but only when is running? Do you see anything wrong with this behavior?

2) The other option that I'm seeing is to let users decide to open with my application or restore default association ... something like: "capture all .lala files" or "restore .lala association". How can I do this? What do you think that is the best approach?

Thanks for your time. Best regards.

+2  A: 

It can probably be done but I think it would end up being cumbersome. All file type associations are stored in the registry so you would have to write/revert registry entries every time your app starts and stops. Also, depending on how frequently you do it the new associations may not be picked up in Windows explorer immediately.

The associations are listed under HKEY_CLASSES_ROOT in the registry and can be mapped a whole myriad of ways (mime types/progIDs/etc).

Many apps I have seen ask if you want to associate certain file types with the application during install time, and give you the ability to opt-out and leave the current settings.

I don't think I'd recommend "on the fly" file type associations

mjmarsh
+1  A: 

1) you get to define the file types that are in the Open Dialog file type droplist. Outside of that, it's possible to change the filetype default on application open, and then replace during application close, as file type association are just a registry setting.

As for wrong, I wouldn't. First reason is that it's not the standard behavior of applications. The second reason is that if your application or PC exits unexpectedly, you run the risk of not returning the association to it's original setting.

2) Windows by default allows user to choose these options utilizing the right-click and the "open with" command.

Stephen Wrighton
+5  A: 

Regarding file associations, I've wrote an answer earlier that at least covers the "How".

This should also point you to the right direction how to handle backup and restore. With direct registry access through c#, there will be no need to use .reg files, so you are free to back up the previous value however you like in your app, and also restore it from there.

The key question here is: Should you change file associations randomly? At least asking the user up-front would obviously be necessary (as you also indicated).

Furthermore, Vista users with UAC enabled, or non-privileged users of other Windows versions may not have the required permission to change global file associations. The (un)installation procedure of your program may be the only place where this can succeed.

EDIT

As Franci Penov indicated in his answer, there is a way to change local file associations on a per-user basis, even for non-admins (that's why I spoke of "global associations" in the previous paragraph). He also mentioned mentioned why going there is not overly advisable.

Tomalak
The other problem is that if your application crashes, it will leave the extension associated incorrectly. It is really irksome to have a program that changes computer settings like that, and leaves them "corrupted" when crashing
Euro Micelli
+3  A: 

You can implement an "on the fly" file association change by associating a small executable with that file extension that upon start will check if your main application is running and pass the file name to it or if it's not running it'll invoke the "regular" associated application.

The main advantage of this approach is that you need to muck with the registry only once.

The main drawbacks of this approach are: - you need a helper process - the application that "owns" these file extensions can detect the change and complain to the user, prompting "repair" thus getting you out of the picture.

Alternatively, you could change the file association upon your main program start. This will work even for non-admin users. while file associations are stored in HKEY_CLASSES_ROOT, there's a small trick - HKCR is actually a map of both HKEY_LOCAL_MACHINE\SOFTWARE\Classes and HKEY_CURRENT_USER\SOFTWARE\Classes. Thus, you can temporarily register the file extension for the current user in HKCU and "shadow" the original association from HKLM. Of course,

I would advice against this approach though, as it takes one crash in your application to make that association permanent and since very few application know how to deal with file associations in HKCU, chances are it'll be an unrecoverable situation for the original application.

Franci Penov
A: 

Sorry to go cross-thread, but I'm interested in file-associations too. My problem is that there doesn't seem to be a way to register file-type-associations that works for Windows Vista and up, AND for Windows XP and down.

The usual way - the registry hack that's been mentioned here - works for XP, but needs administrator privileges in Vista, and still isn't the right way to go in Vista. In Vista the entire idea of file-associations changed by having the user select "default applications".

So if anyone knows of a way - MS approved code, or a class that implements any and all ways to register file-associations for all Windows versions... Please inform us :)

Led
SO isnt really a forum so cross threading doesnt really work, I would suggest you raise this as a question, otherwise you have no way to accept/reward answers
Pharabus