0

I'm trying write to a text file with my function which works fine on Windows Machine but doesn't create the txt file on Android.

public async void WriteTextToFile(string text, string targetFileName)
{
    string targetFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, targetFileName + ".txt");
    using FileStream outputStream = System.IO.File.OpenWrite(targetFile);
    using StreamWriter streamWriter = new StreamWriter(outputStream);
    await streamWriter.WriteAsync(text);
}
Alexandar May - MSFT
  • 6,536
  • 1
  • 8
  • 15
123
  • 39
  • 5
  • 1
    do you get an error or exception? How do you know it's not writing the file? – Jason Dec 14 '22 at 16:36
  • I don't get any error, but when I try to look my txt up in the file manager I can't find it. @Jason – 123 Dec 14 '22 at 16:44
  • 1
    file manager doesn't generally allow you to see files in an app's Data/Documents folder. If you want a file to be visible to other apps you need to use a shared folder – Jason Dec 14 '22 at 16:47
  • I was trying to find it at /data/user/0/com.companyname.mauiapp/files/2022-14-12--16-09-48.txt. If I change the path to a shared folder then can I find it in the file manager? – 123 Dec 14 '22 at 16:49
  • 1
    As an alternative verification, you could write another function, which attempts to open that file path and read from it. – ToolmakerSteve Dec 14 '22 at 23:27
  • The `/data/user/0/com.companyname.mauiapp/files/2022-14-12--16-09-48.txt.` you created is located in the android device, so you can't find it in the file manager in your PC. As Jason suggested, you need to use a shared folder. – Alexandar May - MSFT Dec 15 '22 at 08:36
  • @123 What is the point of all this? What are you trying to do with this text file? – H.A.H. Dec 15 '22 at 14:58

0 Answers0