首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用C#从Windows\System32目录删除文件

使用C#从Windows\System32目录删除文件
EN

Stack Overflow用户
提问于 2011-07-07 04:24:25
回答 3查看 3.1K关注 0票数 1

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

代码语言:javascript
复制
File.Delete(@"c:\windows\system32\<file>");

这不起作用,它不会抛出异常,但也不会删除文件。我认为这与权限有关,但我不确定如何修复它。你能帮上忙吗?

EN

回答 3

Stack Overflow用户

发布于 2011-07-07 04:53:38

好吧,让我们假设你不是在做一些恶意的事情;)不管怎样,我还没有尝试过,但是模拟会有所帮助。

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

票数 2
EN

Stack Overflow用户

发布于 2011-07-07 04:31:50

如果你在Vista或7(或服务器2008+)上这样做,UAC也会阻碍你的删除。在这种情况下,您需要修改应用程序的清单,以便它在启动(或启动提升的子应用程序或进程)时提升其权限:

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

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

票数 1
EN

Stack Overflow用户

发布于 2011-07-07 05:08:31

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

代码语言:javascript
复制
<?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6602487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档