2

I'm facing issue to remove full directory because some file get this error System.UnauthorizedAccessException: 'Access to the path 'pstore.bin' is denied.'

I've tried to give permission to single File but same, tested this but doesn't work too:

var di = new DirectoryInfo(item);
di.Attributes &= ~FileAttributes.Normal;
Directory.Delete(item, true);

Not sure how I can remove this file, I've tested almost everything I found on this forum, and not sure what is wrong. To be clear this file is from Android Studio AVD Emulator.

Updating exception notes:

**System.UnauthorizedAccessException: 'Access to the path 'pstore.bin' is denied.'**
This exception was originally thrown at this call stack:
    [External Code]
    TwitterSuite_v._1._0.Form1.fullZipAll.AnonymousMethod__0(string) in Form1.cs
    [External Code]

1 Answers1

0

To make the C# application run with administrator privileges, add a manifest file (*.manifest) to the application. To create manifest file, follow this path in Visual Studio IDE:

  • Project > Add New Item > Application Manifest File

For the application to run with administrative privileges, you need to add the element <requestedExecutionLevel> in the app.manifest file:

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
    <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">

                <!-- In order for the program to run with administrator privileges, you need to add the following line to the *.manifest file. -->
                <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        
            </requestedPrivileges>
        </security>
    </trustInfo>

    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <application></application>
    </compatibility>
</assembly>

If this solution doesn't work for you, get the error code in the thrown exception and update the question:

try
{
    var di = new DirectoryInfo(item);
    di.Attributes &= ~FileAttributes.Normal;
    Directory.Delete(item, true);
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

Try to verify for what reason this error is occurring:

References
Sercan
  • 4,739
  • 3
  • 17
  • 36