IceCold
IceCold

Reputation: 21134

How to change file association programmatically without require elevation

How to change file association programmatically when the user does not have admin/elevated rights (Win XP, Vista, 7)? Any ideas about how to work around this? Basically I would like to keep my application as lite as it is now (it doesn't need elevated rights to install and run). At the moment I offer a GUI interface where the user can change the file association, but if the user has limited rights all it does is to show a message that it cannot do that and it explains to it how to activate the "Run this program as administrator" box then restart the program. If the user has the rights, then I just change the association.

There is a better way to do it and stay 'lite'?

Upvotes: 4

Views: 5455

Answers (4)

jachguate
jachguate

Reputation: 17203

In Windows (since windows 2000) you're allowed to have system-wide file association, which require elevated privileges to be set, and per user file associations.

If you want to stay lite, make a per_user file association and that's it.

Take a look on this article: Changes in File Types and File Association Features in Windows 2000 and Windows Server 2003.

Upvotes: 10

IceCold
IceCold

Reputation: 21134

My solution (waiting for better alternatives):

It looks like only the admin can change the association globally. In this light, the best way I can imagine now (but not even by far perfect) is to create a small external utility that implicitly runs with elevated rights. This tool will then change the association. Of course the users without elevated rights will still be unable to change the association.

Upvotes: 1

volvox
volvox

Reputation: 1234

You can find a solution at this place using the registry (OS is Windows XP) - so it may not be applicable to your request : http://volvox.wordpress.com/2006/06/02/extensions-101/ - Sorry it's in french ... Complete sources (well documented) and executable to download.

Upvotes: 0

Andrew T Finnell
Andrew T Finnell

Reputation: 13628

You can use the ShellExecute to spawn your external utility. Make sure to include the Shield icon on your action to indicate it will require elevated permissions. It will then prompt the user and let them know it requires special permissions.

One thing you could do is add flags to your own application that indicate it will be changing permissions. And then run your application again, with the special flags.

For example if your application is

MyApplication.exe

you can spawn

MyApplication.exe /setfiles

which would only set the file associations then exit. That way you only have to ship one executable.

function RunAsAdmin(hWnd: HWND; filename: string; Parameters: string): Boolean;
var
    sei: TShellExecuteInfo;
begin
    ZeroMemory(@sei, SizeOf(sei));
    sei.cbSize := SizeOf(TShellExecuteInfo);
    sei.Wnd := hwnd;
    sei.fMask := SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI;
    sei.lpVerb := PChar('runas');
    sei.lpFile := PChar(Filename); // PAnsiChar;
    if parameters <> '' then
        sei.lpParameters := PChar(parameters); // PAnsiChar;
    sei.nShow := SW_SHOWNORMAL; //Integer;
    Result := ShellExecuteEx(@sei);
end;

Upvotes: 2

Related Questions