要求用户使用管理权限重新启动应用程序,就像 VS 调试器那样
我正在开发一个使用旧版 USB 驱动程序的应用程序。由于某种原因,旧版 USB 驱动程序的开发人员决定将信息写入注册表,此过程要求我拥有应用程序的管理权限。
现在,使用注册表的 USB 功能应该只由用户执行一次,或者当他想要重新校准他的设备时,这样我就不需要一直需要管理权限。
当您想要调试需要 Visual Studio 管理权限的应用程序时,您会收到一个弹出窗口,其中请求使用正确的凭据重新启动应用程序。我怎样才能自己做呢?我想正常启动我的应用程序,但是当用户进入校准菜单时,我想向他显示一个类似的弹出窗口,告诉他对于此选项,他必须使用管理权限重新启动应用程序。
I am in the process of developing an application which uses a legacy USB driver. For some reason the developers of the legacy USB driver decided to write information to the registry and this process requires me to have administrative privileges for my application.
Now, the USB functionality that uses the registry should be only done by a user once, or when he wants to recalibrate his device so I don't need administrative privileges all the time.
When you want to debug an application which requires administrative privileges from Visual Studio you get a pop-up with a request to restart the application with the correct credentials. How can I make that myself? I want to start my application normally, but when the user goes to the calibration menu I want to present him with a similar pop-up telling him that for this option he has to restart the application with administrative privileges.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
ProcessStartInfo
类型并将Verb
属性设置为“runas”,要求程序以管理员权限启动。这将提示用户使用标准 Windows 对话框来运行管理代码。
You can ask a program to start up with Admin privileges by using the
ProcessStartInfo
type and setting theVerb
property to "runas".This will prompt the user with the standard Windows dialog for running admin code.
Visual Studio 不会以管理员身份重新启动,它只是检测它是否以管理员身份运行,如果没有,则显示一个消息框。对您来说最简单的测试是调用
IsInRole
- 即使您位于管理员组中,如果应用程序运行在非提升状态,它也会返回 false。例如:您可以放置自己的“请关闭并重新启动我”消息框,而不是这些消息框。或者,如果您想炫耀,请给他们一个按钮,单击该按钮将启动提升的实例(使用 ProcessStart 和 runas)并关闭该实例。
Visual Studio doesn't restart itself as admin, it just detects whether it's running as admin or not and if not, puts up a message box. Simplest test for you would be to call
IsInRole
- even when you're in the Administrators group, if the app is running non elevated it returns false. For example:Rather than these message boxes, you could put your own about "please close and restart me". Or if you want to show off, give them a button to click that will launch an elevated instance (using ProcessStart and runas) and close this one.