1

I get this error when trying to delete a file on AppData\...

System.UnauthorizedAccessException: Access to the path '...AppData\Roaming\Mozilla\Firefox\Profiles\4peif3cq.default\extensions\om.brunolm@gmail.com.xpi' is denied.

if (File.Exists(extFile))
{
    File.Delete(extFile);
}

I've tried to add FullControl permission to Everyone, but the same error happens. The file is not readonly.

Why can't I delete this file with C# code? How can I delete it?

BrunoLM
  • 94,090
  • 80
  • 289
  • 441

2 Answers2

2

Try the same with elevated privileges:

  • First, run your app under admin (Run as administrator in file's context menu).

  • If that helped, add this to your 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="MyApplication.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>
    

See also

Community
  • 1
  • 1
abatishchev
  • 95,331
  • 80
  • 293
  • 426
1

Do you also have full control on the folder that contains a file? Can you view effective permissions under advanced security settings? Or possibly someone else is using that file, that will deny access no matter what.

fejesjoco
  • 11,577
  • 3
  • 32
  • 63
  • Actually Firefox was still open using the file. So it was denying access no matter what :P. Thanks. – BrunoLM Dec 24 '10 at 12:47