Search code examples
c#maui.net-maui

How to get path to MauiAsset on android


I am looking to get the path to a file in my .net Maui app. I have set the file 'Build Action' to MauiAsset and 'Copy to Output Directory' to Copy if newer.

When deploying to Windows I can successfully get the path by using:

string fileName = $"{AppDomain.CurrentDomain.BaseDirectory}MyFolder\\myfile.txt"; 

which resolves as: "C:\Users\xxx\source\repos\myproject\myproject\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\MyFolder\myfile.txt"

On android im trying to use:

string fileName = $"{AppDomain.CurrentDomain.BaseDirectory}/MyFolder/myfile.txt";

which resolves as: "/data/user/0/com.companyname.myproject/files/MyFolder/myfile.txt"

This is aparently not a usable path on android as the app then throws

System.IO.DirectoryNotFoundException: 'Could not find a part of the path '/data/user/0/com.companyname.myproject/files/MyFolder/myfile.txt'.'

when I try to access it using

System.IO.File.ReadAllLines(fileName);

It also looks like the start of the path is missing but how do I get that?


Solution

  • Solution

    Solution: So I finally found a solution to this. I have tried adding the MauiAsset in the project file (myproject.csproj) with the exact path as:

    <ItemGroup>
    <MauiAsset Include="MyFolder\myfile.txt" />
    </ItemGroup>
    

    Which simply DOES NOT WORK no matter how you twist and turn it. The only thing I can get to work is copying Microsoft's recursive folder include as:

    <MauiAsset Include="MyFolder\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
    

    and then in c# set the filepath of the file located inside MyFolder as

    string filePath = "myfile.txt"
    

    Continue to the update section on details about reading.

    Update

    After going through the file system API as shown: https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/storage/file-system-helpers?tabs=android

    I think you need to use the OpenAppPackageFileAsync:

    Something like below:

       public async Task<string> ReadTextFile(string filePath)
       {
          using Stream fileStream = await FileSystem.Current.OpenAppPackageFileAsync(filePath);
          using StreamReader reader = new StreamReader(fileStream);
          return await reader.ReadToEndAsync();
       }
    

    OG ANSWER

    One lesson that I have learned while using Xamarin also applies to Maui(probably). That handling path Cross-platform is annoying as hell, One incorrect move and you are doomed to find what you did wrong. What I like to do to avoid these mistakes is let C# handle based on Target Platform on how to get the correct path for my file(Not 100% foolproof but definitely easier).

    Use Path.Combine to get your path to avoid issues with the different OS using different "/" "" kinds of brackets:

    In your case, all you do is:

    var fullPath = System.IO.Path.Combine(FileSystem.AppDataDirectory,"MyFolder","myfile.txt");