1

I'm developing a C# application that will need to delete a couple of files in System32, and I'm doing the following:

File.Delete(@"c:\windows\system32\<file>");

This isn't working, it doesn't throw an exception but it also doesn't delete the file. I'm thinking it's related to the permissions, but I'm not sure how to fix it. Can you help?

4

3 回答 3

2

好吧,让我们假设您没有做恶意的事情;)无论如何,还没有尝试过,但是模拟会有所帮助。

Google impersonation c# 你会看到很多例子,邮件的想法很简单:你的代码通常在你的用户权限下运行。通过模拟,您可以在另一个用户的权限下运行您的代码(以编程方式,用户不需要做任何事情)。因此,如果用户可以在没有 UAC 限制的情况下直接访问该文件夹,那么理论上它应该在那时运行。但同样,我还没有尝试过,所以如果它不起作用,请不要生气。只是一个想法。

于 2011-07-06T20:53:37.577 回答
1

如果您在 Vista 或 7(或 Server 2008+)上执行此操作,UAC 也会妨碍您的删除。在这种情况下,您需要修改应用程序的清单,以便它在启动时提升其权限(或启动提升的子应用程序或进程):

http://victorhurdugaci.com/using-uac-with-c-part-1/

此外,如果您发布了您遇到的异常,这将很有帮助,因为这将表明它是与权限相关、与 x64 相关还是与 UAC 相关。

于 2011-07-06T20:31:49.537 回答
0

您需要管理权限才能修改该文件夹中的文件。app.manifest在属性中使用文件,如下所示:

<?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="YourApplication.app" />
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
      <applicationRequestMinimum>
        <defaultAssemblyRequest permissionSetReference="Custom" />
        <PermissionSet ID="Custom" SameSite="site" Unrestricted="true" />
      </applicationRequestMinimum>
    </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>
  <!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
  <!-- <dependency>
    <dependentAssembly>
      <assemblyIdentity
          type="win32"
          name="Microsoft.Windows.Common-Controls"
          version="6.0.0.0"
          processorArchitecture="*"
          publicKeyToken="789cf14ab782c1eb"
          language="*"
        />
    </dependentAssembly>
  </dependency>-->
</asmv1:assembly>
于 2011-07-06T21:08:31.497 回答