Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

NullReferenceException - File.WriteAllText Xamarin/Android ?

By M.K. Verma · 4 May 2022

NullReferenceException - File.WriteAllText Xamarin/Android ?

Try the following code

//file path
var filePath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "RecentHosts.txt");
if (!File.Exists(filePath))
{
    File.Create(filePath);
}

string text = JsonConvert.SerializeObject(new JsonHosts() { hosts = RecentHosts.Values.ToList() });
//write in file
File.WriteAllText(filePath, text);