【问题标题】:Delete File from Windows\System32 directory using C#使用 C# 从 Windows\System32 目录中删除文件
【发布时间】:2011-07-06 20:24:24
【问题描述】:

我正在开发一个需要删除 System32 中的几个文件的 C# 应用程序,我正在执行以下操作:

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

这不起作用,它不会引发异常,但也不会删除文件。我认为它与权限有关,但我不确定如何解决它。你能帮忙吗?

【问题讨论】:

  • 我很难想象从 system32 目录中删除某些内容的正当理由。你想完成什么?
  • 您是否有机会在 x64 计算机上使用 x86?
  • 我刚刚尝试删除我自己在 System32 文件夹中创建的一个示例 .txt 文件并得到了 UnauthorizedAccessException...不知道你为什么不这样做?
  • 这可能与 UAC 相关,但没有关于您尝试做什么的背景信息很难说。

标签: c# .net windows delete-file system32


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

      您需要管理员权限才能修改该文件夹中的文件。在属性中使用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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-09
        • 1970-01-01
        • 1970-01-01
        • 2016-12-27
        相关资源
        最近更新 更多