WPF应用程序未获得文件访问权限

时间:2011-02-09 14:17:10

标签: wpf permissions file-permissions

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

2 个答案:

答案 0 :(得分:2)

通常,.Net希望您将应用程序生成的文件放入用户的主目录或共享用户文件夹中。看看这个答案:When using a Settings.settings file in .NET, where is the config actually stored? 它讨论.Net配置文件,但你可以把其他文件放在那里。

答案 1 :(得分:1)

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

清单示例(requestedExecutionLevel部分是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>