0

我正在尝试编写一个控制台应用程序来使用 .net File.Delete 从 Windows\System32 中删除文件。该应用程序在 Windows 7 上运行,但由于据称找不到文件而失败。

我研究并发现这是框架的安全限制,但我也在此处对类似问题的回答中发现,如果我将清单文件添加到我的解决方案中,并对其进行编辑以使其包含

<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

然后每当我启动应用程序时,系统都会提示我输入用户名和密码以“证明”我是管理员,然后应用程序将在 System32 中找到文件并按预期删除它们。

这不会发生。没有提示我输入 u\p 并且程序失败。我试过调试\发布\32位\64位。

有什么建议吗?

完整清单:

<?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="EclCleaner.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>
</asmv1:assembly>
4

3 回答 3

1

尴尬。在 64 位操作系统上运行并在 X86 中编译代码时,.net 框架会将与 Windows\System32 相关的每个请求重定向到 WoW64 文件夹。尽管在调试中路径的值为 system32,但它实际上是在查看不同的文件夹。

于 2014-06-05T23:18:51.793 回答
0

正如您在自己的回答中所说,Windows 会自动且透明地将请求重定向到Windows\System32要成为文件夹的Windows\WoW64文件夹。这是为了防止使用硬编码字符串的程序加载 system32 文件夹中的 64 位 dll,这会导致程序崩溃。

如果要从 32 位应用程序导航到 64 位系统文件夹,则需要使用隐藏文件夹Windows\sysnative,它指向 64 位 system32 文件夹。重要提示,该文件夹只能从 32 位程序中看到,如果您尝试从 64 位程序(例如 Windows 资源管理器)连接到该文件夹​​,则该文件夹将不存在。

class Program
{
    static void Main(string[] args)
    {
        //True in 32 bit, false in 64 bit.
        var tmp = Directory.Exists(@"C:\Windows\sysnative");
        Debugger.Break();
    }
}
于 2014-06-06T00:03:18.387 回答
0

如果 UAC 已被禁用,则使用requestedExecutionLevel是不够的。在这种情况下,您需要检测用户是否实际上具有管理员权限。你可以这样做:

if (!IsInRole(WindowsBuiltInRole.Administrator))

如果用户没有管理员权限,则需要自行提升。可以在此处找到如何自我提升的示例。

于 2014-06-04T19:14:18.907 回答