【问题标题】:How to get access to file to delete for emulators files?如何访问文件以删除模拟器文件?
【发布时间】:2022-01-23 07:03:12
【问题描述】:

我面临删除完整目录的问题,因为某些文件出现此错误System.UnauthorizedAccessException: 'Access to the path 'pstore.bin' is denied.'

我试图授予单个文件的权限,但相同,测试过但也不起作用:

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

不知道如何删除这个文件,我已经测试了我在这个论坛上找到的几乎所有内容,但不确定哪里出了问题。需要明确的是,此文件来自 Android Studio AVD Emulator。

更新异常说明:

**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]

【问题讨论】:

    标签: c# file directory delete-file


    【解决方案1】:

    要使 C# 应用程序以 管理员权限 运行,请将清单文件 (*.manifest) 添加到应用程序。要创建清单文件,请在 Visual Studio IDE 中遵循以下路径:

    • Project > Add New Item > Application Manifest File

    要让应用程序以管理权限运行,您需要在app.manifest 文件中添加元素<requestedExecutionLevel>

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

    如果此解决方案不适合您,请在抛出的异常中获取错误代码并更新问题:

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

    尝试验证发生此错误的原因:

    参考文献

    【讨论】:

    • 感谢测试,消息与线程中的相同
    • 遗憾地没有帮助我。您可以检查更新的帖子异常。
    • 以管理员权限运行 Android Studio 和 Visual Studio IDE。当前用户必须对pstore.bin 文件拥有“完全控制”权限。
    • 但我通过 avdmanager 创建模拟器,并通过命令启动它们。那么,如果仅通过管理员权限启动 Visual Studio?这应该有帮助吗?
    猜你喜欢
    • 2021-12-21
    • 2015-12-11
    • 2012-09-07
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多