如何以编程方式更改文件关联而不需要提升

发布于 2024-10-09 23:51:21 字数 279 浏览 0 评论 0原文

当用户没有拥有管理员/提升权限时(Win XP、Vista、7),如何以编程方式更改文件关联?关于如何解决这个问题有什么想法吗?基本上我希望我的应用程序保持像现在一样精简(它不需要提升的权限来安装和运行)。 目前,我提供了一个 GUI 界面,用户可以在其中更改文件关联,但如果用户的权限有限,它所做的只是显示一条消息,表明它无法执行此操作,并向其解释如何激活“运行此程序”以管理员身份”框,然后重新启动程序。如果用户有权限,那么我只需更改关联即可。

有更好的方法来做到这一点并保持“精简”吗?

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'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

肩上的翅膀 2024-10-16 23:51:21

在 Windows(自 Windows 2000 起)中,您可以进行系统范围的文件关联(这需要设置提升的权限)以及每个用户的文件关联。

如果您想保持精简版,只需建立一个 per_user 文件关联即可。

查看这篇文章:文件中的更改Windows 2000 和 Windows Server 2003 中的类型和文件关联功能

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.

梦情居士 2024-10-16 23:51:21

您可以使用 ShellExecute 生成外部实用程序。确保在您的操作中包含盾牌图标,以表明它需要提升的权限。然后它会提示用户并让他们知道它需要特殊权限。

您可以做的一件事是向您自己的应用程序添加标志,表明它将更改权限。然后使用特殊标志再次运行您的应用程序。

例如,如果您的应用程序是

MyApplication.exe,

您可以生成

MyApplication.exe /setfiles

,这只会设置文件关联,然后退出。这样您只需发送一个可执行文件。

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;

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;
北方的巷 2024-10-16 23:51:21

我的解决方案(等待更好的替代方案):

看起来只有管理员可以全局更改关联。从这个角度来看,我现在能想象的最好的方法(但甚至还不是完美的)是创建一个小的外部实用程序,隐式地以更高的权限运行。然后该工具将更改关联。当然,没有提升权限的用户仍然无法更改关联。

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.

奈何桥上唱咆哮 2024-10-16 23:51:21

您可以使用注册表在此处找到解决方案(操作系统是 Windows XP) - 因此它可能不适用于您的请求: http://volvox.wordpress.com/2006/06/02/extensions-101/ - 抱歉,它是法语...完整的源代码(有据可查)和可执行文件下载。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文