WPF应用程序未获取文件访问权限 [英] WPF Application not getting file access rights

查看:264
本文介绍了WPF应用程序未获取文件访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,可以在其自己的安装目录中创建一些文本文件.但是,即使在UAC提示之后,Windows Vista和Windows 7用户通常仍然经常会遇到文件访问失败"类型错误.解决方案是在Windows资源管理器中找到可执行文件,并在文件属性下打开兼容性"选项卡,然后选中以管理员身份运行".这显然是一种糟糕的用户体验,但是我不确定如何确保应用程序能够在不采取此步骤的情况下保护自己的这些权限.我不是要绕过UAC提示.

I have a WPF app that creates some text files in its own install directory. However, even after the UAC prompt, windows vista and windows 7 users often times still get "file access failed" type errors. The solution is to find the executable in windows explorer and open up the compatibility tab under the file properties and check "run as administrator". This is obviously a terrible user experience but I'm not sure how to ensure the app can secure itself these permissions without that step being taken. I am not trying to bypass the UAC prompts.

推荐答案

您可以通过嵌入自定义清单(项目属性->构建->清单)来强制您的应用以管理员权限开始(无论如何,UAC都会显示其对话框).

You can force your app to start with admin rights (UAC will show it's dialog box anyway) by embedding custom manifest (project properties -> build -> Manifest).

清单示例(requestedExecutionLevel部分是importaint):

Manifest example (requestedExecutionLevel part is importaint):

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0"
  xmlns="urn:schemas-microsoft-com:asm.v1"
  xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
  xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <assemblyIdentity version="1.0.0.0" name="yourappname.app"/>
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
            <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
                <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
            </requestedPrivileges>
        </security>
    </trustInfo>
    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <application>
            <!-- A list of all Windows versions that this application is designed to work with. Windows will automatically select the most compatible environment.-->
            <!-- If your application is designed to work with Windows 7, uncomment the following supportedOS node-->
            <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>-->
        </application>
    </compatibility>
</asmv1:assembly>

这篇关于WPF应用程序未获取文件访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆