C# 如果路径受保护,请求 Windows Vista UAC 提升?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Request Windows Vista UAC elevation if path is protected?
提问by sieben
For my C# app, I don't want to always prompt for elevation on application start, but if they choose an output path that is UAC protected then I need to request elevation.
对于我的 C# 应用程序,我不想总是在应用程序启动时提示提升,但如果他们选择受 UAC 保护的输出路径,那么我需要请求提升。
So, how do I check if a path is UAC protected and then how do I request elevation mid-execution?
那么,如何检查路径是否受 UAC 保护,然后如何在执行过程中请求提升?
采纳答案by Adrian Clark
The best way to detect if they are unable to perform an action is to attempt it and catch the UnauthorizedAccessException
.
检测他们是否无法执行操作的最佳方法是尝试并捕获UnauthorizedAccessException
.
However as @DannySmurfcorrectly points outyou can only elevate a COM object or separate process.
但是,正如@ DannySmurf正确指出的那样,您只能提升 COM 对象或单独的进程。
There is a demonstration application within the Windows SDK Cross Technology Samples called UAC Demo. This demonstration application shows a method of executing actions with an elevated process. It also demonstrates how to find out if a user is currently an administrator.
Windows SDK 跨技术示例中有一个名为UAC Demo的演示应用程序。此演示应用程序展示了一种使用提升进程执行操作的方法。它还演示了如何确定用户当前是否为管理员。
回答by TheSmurf
Requesting elevation mid-execution requires that you either:
请求提升中间执行要求您:
- Use a COM control that's elevated, which will put up a prompt
- Start a second process that is elevated from the start.
- 使用提升的 COM 控件,它将显示提示
- 启动从一开始就提升的第二个进程。
In .NET, there is currently no way to elevate a running process; you have to do one of the hackery things above, but all that does is give the user the appearance that the current process is being elevated.
在 .NET 中,目前没有办法提升正在运行的进程;你必须做上面的其中一件骇人听闻的事情,但所做的一切只是让用户感觉当前进程正在被提升。
The only way I can think of to check if a path is UAC elevated is to try to do some trivial write to it while you're in an un-elevated state, catch the exception, elevate and try again.
我能想到的检查路径是否被 UAC 提升的唯一方法是在您处于未提升状态时尝试对其进行一些简单的写入,捕获异常,提升并重试。
回答by Brian Leahy
You may want to notify the user that the path is protected and ask them to output the file to a "safer" area. This way your app will not need elevation. I'm sure it depends on your users and what you are trying to do, however I don't think it's too much to kindly let the user know you don't feel ok dumping xyz into the Windows/System32 folder.
您可能希望通知用户该路径受到保护,并要求他们将文件输出到“更安全”的区域。这样你的应用就不需要提升了。我确定这取决于您的用户以及您要尝试做什么,但是我认为让用户知道您将 xyz 转储到 Windows/System32 文件夹中感觉不太好。
回答by Brian Leahy
If your secondary drive has it's own file permissions, like say you have an other copy of windows installed on it. It will prompt.
如果您的辅助驱动器具有自己的文件权限,例如您安装了其他 Windows 副本。它会提示。
It will also prompt if files are in use, which sometimes occurs if you have windows explorer open to the same directory and the file selected with a file previewer displaying the contents... there are some other oddities, but generally you get asked for file permission if the file is in use or it's a sensitive directory.
它还会提示是否正在使用文件,如果您将 Windows 资源管理器打开到同一目录并且使用显示内容的文件预览器选择文件,有时会发生这种情况……还有一些其他奇怪的事情,但通常会要求您提供文件如果文件正在使用或它是敏感目录,则授予权限。
If you do loop the FolderBrowserDialog , make sure to notify the user why, so they dont get mad at your app.
如果您确实循环了 FolderBrowserDialog ,请确保通知用户原因,这样他们就不会对您的应用程序生气。
Note: it does stink there is no .net way of asking for permission, maybe p/invoke the win32 api...?
注意:它确实很糟糕,没有 .net 请求许可的方式,也许 p/invoke the win32 api...?
回答by Tristan
UAC can elevate object based on their GUID, this would (In theory) mean that any class with a GUID can be elevated, The UACDemo should also show how to do this
UAC 可以根据其 GUID 提升对象,这(理论上)意味着可以提升任何具有 GUID 的类,UACDemo 还应该展示如何做到这一点
回答by mosu
I'm not sure if it is of any help for you but you can take a look at this blog post:
我不确定它是否对你有帮助,但你可以看看这篇博文:
http://haishibai.blogspot.com/2010/01/tiy-try-out-windows-7-uac-using-c-part_26.html
http://haishibai.blogspot.com/2010/01/tiy-try-out-windows-7-uac-using-c-part_26.html