【问题标题】:WPF Application not getting file access rightsWPF应用程序没有获得文件访问权限
【发布时间】:2011-06-24 04:20:22
【问题描述】:

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

【问题讨论】:

    标签: wpf permissions file-permissions


    【解决方案1】:

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

    【讨论】:

    • 谢谢,但我不同意这里的 .net。原因是文件位置会根据 Windows 版本而改变,这会产生一些支持噩梦,因为我的用户不断创建和共享文件。将它们定向到 c:\program files\myapp\etc 比说“if xp then”容易得多
    • 但是您知道程序文件目录也不是固定的,不是吗?它会随着 64/32 位系统、语言等而改变,安装目录本身当然也是可以改变的。但无论什么最适合你... :-)
    【解决方案2】:

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

    清单示例(requestedExecutionLevel 部分很重要):

    <?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>
    

    【讨论】:

      猜你喜欢
      • 2013-05-02
      • 2016-08-18
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      相关资源
      最近更新 更多