如何在Xamarin android mono中以编程方式清除我的应用程序的捕获数据

ROH*_*APU 1 xamarin.android xamarin

我曾尝试使用下面的代码,我能够获取catch文件的路径,但无法删除路径或该路径中的数据.下面是我用来做的代码.

public void clearApplicationData()
        {
            var tmpdir = System.IO.Path.GetTempPath();
             File applicationDirectory = new File(tmpdir);
              if (applicationDirectory != null && applicationDirectory.Exists())
            {
                //deleteFile(applicationDirectory);
                string[] fileNames = applicationDirectory.List();
                foreach (string fileName in fileNames)
                {
                    if (!fileNames.Equals("lib"))
                    {
                        deleteFile(new File(applicationDirectory, fileName));
                    }
                }
            }
        }
 public static bool deleteFile(File file)
        {
            bool deletedAll = false;
            if (file != null)
            {
                if (file.IsDirectory)
                {
                    string[] children = file.List();
                    for (int i = 0; i < children.Length; i++)
                    {
                        deletedAll = deleteFile(new File(file, children[i])) && deletedAll;
                    }
                }
                else
                {
                    file.DeleteOnExit();
                    deletedAll = file.Exists();
                }
            }

            return deletedAll;
        }
Run Code Online (Sandbox Code Playgroud)

我还添加了CLEAR_APP_CATCH的权限,CLEAR_APP_USER_DATA请帮我这样做,我愿意清除完整的应用程序现金和数据,最后我愿意重新启动应用程序以显示登录页面或结束应用程序.

jta*_*agg 7

这应该立即清除所有数据和缓存.它将关闭应用程序并释放内存.

((ActivityManager)Application.Context.GetSystemService(ActivityService)).ClearApplicationUserData();
Run Code Online (Sandbox Code Playgroud)

如果不需要,您可以尝试以下代码.

另外作为旁注,我建议您使用System.IO文件实用程序而不是Java.IO文件.

try
{
    var cachePath = System.IO.Path.GetTempPath();

    // If exist, delete the cache directory and everything in it recursivly
    if (System.IO.Directory.Exists(cachePath))
        System.IO.Directory.Delete(cachePath, true);

    // If not exist, restore just the directory that was deleted
    if (!System.IO.Directory.Exists(cachePath))
        System.IO.Directory.CreateDirectory(cachePath);
}
catch(Exception){}
Run Code Online (Sandbox Code Playgroud)

您可以尝试使用相同的东西来删除应用程序数据

var dataPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
Run Code Online (Sandbox Code Playgroud)

代替cachePath,但请记住,您的应用程序可能表现得很有趣,因为它仍然在堆内存中有信息并且缺少对应的文件.例如,除此之外,您可能还想清除SharedPref.