.NET Maui File Locations

Fritz Switzer 261 Reputation points
2022-07-22T14:42:03.13+00:00

I've used code that is similar to the following in my UWP programs:

 string filename = "Northwind.db";  
        bool isExisting = false;  
        try  
        {  
            StorageFile storage = await Application.Current.LocalFolder.GetFileAsync(filename);  
            isExisting = true;  
        }  
        catch (Exception)  
        {  
            isExisting = false;  
        }  
        if (!isExisting)  
        {  
            StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync(filename);  
            await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder, filename, NameCollisionOption.ReplaceExisting);  
        }  

but this doesn't work with my .NET Maui program. I am mostly interested in the Windows and Android projects. There are so many different File location options. The key point is I want to use an existing SQLite db with my app.

I've tried to assign the db to MauiAsset, but it goes to a local location.

Universal Windows Platform (UWP)
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,672 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,412 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Gordon Moore 6 Reputation points
    2022-07-23T09:20:51.41+00:00

    private static Database.Database database;
    private readonly static string filename = "xxx.db3";

    public static Database.Database Database
    {
    get
    {
    if (database == null)
    {
    database = new Database.Database(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), filename));
    }
    return database;
    }
    }

    This does work for Android (Taken from gerald Versluis video https://www.youtube.com/watch?v=uxqQqyuZ3Qo&t=1003s

    To find the db3 you can use Android Studio and the Device File Explorer - look under Data and then drill down to your company website app name e.g. uk.co.xxxxx.appname

    However, I too am quite keen to know about all the different locations, because i'd rather my db3 was in something more accessible.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.