File.Copy UnauthorizedAccessException

发布于 2025-01-05 23:17:14 字数 733 浏览 3 评论 0原文

我有一个应用程序需要将一些文件复制到 C:\Program Files 下的目录。

在启用了 UAC 的 Windows 7 上,我在调用 File.Copy 时收到 UnauthorizedAccessException。

我已将应用程序清单添加到我的应用程序中,如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
            level="requireAdministrator"
            uiAccess="false"/>
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>

现在,我在运行应用程序时收到 UAC 提示,但它仍然会导致 UnauthorizedAccessException。

有想法吗?

I have an application that needs to copy some files to a directory under C:\Program Files.

On Windows 7 with UAC enabled, I get an UnauthorizedAccessException when calling File.Copy.

I've added a app manifest to my application as follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
            level="requireAdministrator"
            uiAccess="false"/>
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>

I now get a UAC prompt when running the application, but it still results in the UnauthorizedAccessException.

Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱格式化 2025-01-12 23:17:14

像往常一样,这是我做的愚蠢的事情。

有问题的文件是只读的。删除此属性使我的应用程序能够正确工作。

As usual, this was something stupid I was doing.

The files in question were read-only. Removing this attribute allowed my application to work correclty.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文