如何在 Xamarin android mono 中以编程方式清除我的应用程序的捕获数据 [英] How to Clear catch data of my application programmatically in Xamarin android mono

查看:22
本文介绍了如何在 Xamarin android mono 中以编程方式清除我的应用程序的捕获数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试使用以下代码,我能够获取捕获文件的路径,但无法删除该路径或该路径中的数据.以下是我用来执行此操作的代码.

I had tried using the below code, I am able to get the path of the catch file but unable to delete the path or the data in that path. Below is the code I am using to do that.

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;
        }

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

I had also added the permissions for the CLEAR_APP_CATCH, CLEAR_APP_USER_DATA Please help me doing this, I am willing to clear the complete applications cash and data and at end the I am willing to restart the application to show the login page or end application.

推荐答案

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

This should clear all of your data and cache at once. It will close the app and free up the memory.

((ActivityManager)Application.Context.GetSystemService(ActivityService)).ClearApplicationUserData();

如果这不是你想要的,你可以试试下面的代码.

If this is not desired you can try the code below.

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

Also as a side note, I'd recommend that you use System.IO file utilities over Java.IO ones.

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){}

您可以尝试使用相同的方法删除应用程序数据

And you can try the same thing to delete the application data by using

var dataPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);

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

in place of cachePath, but keep in mind that your app might behave funny since it still has information in heap memory and missing the files that correspond. For example you will probably want to clear your SharedPrefs in addition to this.

这篇关于如何在 Xamarin android mono 中以编程方式清除我的应用程序的捕获数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆