如何在 WinForms 中强制用户使用管理员帐户

发布于 2024-08-30 04:11:34 字数 221 浏览 1 评论 0原文

我有简单的 WinForms 应用程序,可以修改 Windows 注册表。问题是在 Vista / Windows 7 中我需要强制用户切换到管理员。

我不想强制用户以管理员身份运行应用程序。我希望他在需要写入注册表时这样做。

最好的情况是当用户需要“切换”到管理员时,达到与许多设置中出现的完全相同的消息,因此无需从管理员身份运行表单开始。

我如何在 .Net 中实现这一目标?

I have simple WinForms application where modifying Windows Registry. The problem is that in Vista / Windows 7 I need to force user to switch to administrator.

I do not want to force user to Run as Administrator form start of the application. I want him to do it when there is necessity to write to registry.

Best case scenario would be to reach exacly the same message which appear in lot's of Setups, when user need to 'switch' to Administrator so there is no necessity to Run as Administrator form beginning.

How I can achieve this in .Net ?

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

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

发布评论

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

评论(3

淡写薰衣草的香 2024-09-06 04:11:34

如果应用程序有时不执行注册表操作而有时执行注册表操作,那么分区是一种可行的方法。分区的三个关键是(1)在第二个 exe 上有一个清单,正如 Ho 所说,(2)在按钮/菜单项上放置一个盾牌,以便用户期望提升,以及(3)使用 ShellExecute 启动它(在调用 Start,将 UseShellExecuteFlag 设置为 true)以便使用清单。

但是,在麻烦地拆分您的应用程序之前,我会问两个问题。首先,它是否曾经用于非管理目的,或者每个用户是否总是“单击该按钮”并需要提升?如果是这样,那么只需将管理清单放在应用程序上,不要对其进行分区。其次,您确定需要写入注册表的该部分吗?你能把你的钥匙移到 HKCU 下的某个地方吗?如果可以的话,那么你就不再需要提升,每个人都会更快乐。我总是喜欢首先考虑这些可能性,因为它们意味着比分区更少的代码和更少的测试。

Partitioning is the way to go if the application sometimes doesn't do the Registry thing and sometimes does. The three keys to partitioning are (1) have a manifest on the second exe, as Ho says, (2) put a shield on the button/menu item so the user expects the elevation, and (3) launch it with ShellExecute (before calling Start, set the UseShellExecuteFlag to true) so that the manifest is used.

However, before going to the trouble of splitting your app, I would ask two questions. First, is it ever used for non administrative purposes or does every user always "click that button" and need to elevate? If so, then just put an admin manifest on the app and don't partition it. Second, are you sure to need to write to that part of the registry? Could you move your key to something under HKCU? If you can, then you don't need elevation any more and everyone's happier. I always like to consider those possibilites first since they mean less code and less testing than partioning does.

听闻余生 2024-09-06 04:11:34

正如 Aaronaught 所说,我认为一个进程不可能请求提升自身。解决此问题的一种方法是将进程分成两个应用程序,一个是执行大部分工作的普通应用程序,另一个仅执行注册表写入操作,而这个应用程序的清单包含类似内容

<requestedExecutionLevel level="requireAdministrator"/>

As Aaronaught says, I don't think it's possible for a process to to request to elevate itself. One way around this is that you split your process into two apps, one is the normal one that does most of the work and the other one only does the registry writes and this one has a manifest that contains something like

<requestedExecutionLevel level="requireAdministrator"/>
無心 2024-09-06 04:11:34

据我所知,没有 API 可以提升进程。当一个进程尝试以提升模式启动另一个进程时,它会自动发生。

这也是它与 Windows Installer 的配合方式。我不确定它是否真的启动了另一个提升的进程,或者只是创建了一个提升的 COM 对象,但它实际上是同一件事。

我个人不会采用这种黑客的解决方法来提升进程的执行中期;如果您的进程可能需要提升,则使用清单明确说明并让同意消息在启动时弹出。但如果您绝对必须这样做,那么就是这样 - 您需要从应用程序内启动一个提升的进程。

As far as I know, there's no API to elevate a process. It happens automatically when a process tries to launch another process in elevated mode.

This is also how it works with the Windows Installer. I'm not sure if it literally starts another elevated process or just creates an elevated COM object, but it's effectively the same thing.

I personally wouldn't resort to this hackish workaround to elevate your process mid-execution; if your process may require elevation, then make that explicit with a manifest and let the consent message pop up on startup. But if you absolutely must do this, then that's how - you need to launch an elevated process from within your app.

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