开发者

Delete File from Windows\System32 directory using C#

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 work开发者_JAVA技巧ing, 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?


Well, let's just assume you are not doing something malicious ;) Anyway, haven't tried it, but Impersonation would help.

Google impersonation c# and you'll see lots of examples, and the mail idea is simple: your code is normally running under the priviledges of your user. By impersonation, you may run your code (programatically, user doesn't need to do anything) under the priviledges of another user. So if a user has direct access to that folder without the UAC restirction, then, theoretically, it should just run then. But again, I haven't tried it, so don't get mad if it doesn't work. Just an idea.


If you're doing this on Vista or 7 (or Server 2008+), UAC will get in the way of your delete as well. In that case, you'll need to modify the manifest of your application so that it elevates its permissions when it starts (or launch an elevated sub-app or process):

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

Also, it would be helpful if you posted the exception you're getting, since that would suggest whether it's permissions-related, x64-related, or UAC.


You need administrative access to modify files in that folder. Use an app.manifest file in properties, like so:

<?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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜