Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Internal Storage of Android in Xamarin?

I want to be able to use Android's internal storage. I found some code snippets like:

 string path = Application.Context.FilesDir.Path;
 var filePath = Path.Combine(path, "test.txt");
 System.IO.File.WriteAllText(filePath, "Hello World");

But I could not figure out what Application is in here.

Is there any solution for that or different perspective? Thanks :)

like image 506
Emrah Akgül Avatar asked Jun 28 '16 12:06

Emrah Akgül


1 Answers

Use this:

string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string filename = System.IO.Path.Combine(path, "testfile.txt");

// Write
using (var streamWriter = new System.IO.StreamWriter(filename, true))
{
    streamWriter.WriteLine(DateTime.UtcNow);
}

// Read
using (var streamReader = new System.IO.StreamReader(filename))
{
    string content = streamReader.ReadToEnd();
    System.Diagnostics.Debug.WriteLine(content);
}
like image 115
jzeferino Avatar answered Oct 05 '22 12:10

jzeferino