Monogame Android get file names from directory

Hello!
I’m trying to create a port of my game project from desktop to android. I have created a solution with three projects:

  • Shared project : which has all the code and Content,
  • Desktop project
  • Android project

I have a function that loads all the images as Texture2D from a specific folder. This works fine for desktop, but for Android it doesn’t work at all.
Here’s the code

  public static List<Texture2D> ImportFolder(string folderName ,string fullPath,ContentManager content)
        {
            List<Texture2D> temp = new List<Texture2D>();
            DirectoryInfo d = new DirectoryInfo(fullPath);
            FileInfo[] Files = d.GetFiles("*.png"); //Getting png files

            foreach (FileInfo file in Files)
            {
                //cut out .png
                string[] tempString = file.Name.Split('.');

                string fileName = tempString[0];
                string finalName = folderName + '/' + fileName;
                temp.Add(content.Load<Texture2D>(finalName));
            }
            
            return temp;

        }

Where does Android store the Content files? How do I get the names of the files from a directory?
Thanks in advance :slight_smile:

1 Like

This works for me loading files from the Contents folder on both Android and iOS:

var filePath = Path.Combine(Content.RootDirectory, "Map/level.json");
using (Stream stream = TitleContainer.OpenStream(filePath))
{
         world = mapSerializer.Load(stream);
}
2 Likes

Yes, but I do not have the file names, only the directory name. Will try to work with this though, thanks for helping :smiley:

I’m beginning to think that nothing works and that I will have to just create a dictionary or an XML file with all my file names in it. Loop through that and load the content that way… which fucking sucks, because this is easily done in every other language I have programmed with before.

I haven’t tried it on Android but on other platforms I use AppDomain.CurrentDomain.BaseDirectory to get the directory where the game’s executable is.

You could try something like:

Directory.GetFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content/"));

No idea if this is the right track to look into.

Edit: Actually looking at the MonoGame code: https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Platform/TitleContainer.Android.cs, there’s Android.App.Application.Context.Assets.

That appears to be: https://docs.microsoft.com/en-us/dotnet/api/android.content.res.assetmanager.

I think what you want is List: https://docs.microsoft.com/en-us/dotnet/api/android.content.res.assetmanager.list.

2 Likes

Thank you so much for helping. I have “solved” this issue by putting the file names into a dictionary with the folder name as the key, the value is an array of file names.
I then loop through the dictionary and load all the Textures into another dictionary. It’s not ideal, but hey it works :stuck_out_tongue:
Hope that your links will help out somebody in the future.

1 Like