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;
}
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With