3

I'm trying to read a File in my Shared Project.

        var currentPath = Environment.CurrentDirectory;
        var filename = Path.Combine(currentPath, "File.txt");
        var content = File.ReadAllText(filename);
        return content;

If i start my application in iOS this works fine, but in Android i get the FileNotFound exception.

Korken55
  • 233
  • 1
  • 3
  • 14

1 Answers1

0

The file system structure and the underlying APIs vary from platform to platform. Environment.CurrentDirectory exists on iOS, but it does not exist on Android nor on UWP, so it will have a null value, and therefore it won't be able to find the file.

Because the file system is so different between platforms it is recommended you abstract environment level file accessing code to platform specific projects instead of shared ones - although you could make it work with compiler directives.

#if __ANDROID__
var currentPath = Environment.DataDirectory;
#endif

Alternatively, you can experiment getting the current directory from the Directory.GetCurrentDirectory(); method (in the System.IO namespace)

blas3nik
  • 1,381
  • 11
  • 21