0

I'm trying to add a SQLite database to my .NET MAUI app, but cannot get the path of the database file.

When I add a break point at this code

string dbpath = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
    "clientdb.db3");

the dbpath variable is null when debugging the app and the table method does not display anything.

I've tried changing LocalApplictionData into CommonApplicationData and also MyDocuments, but it does not work. the rest of the code is:

   public SQLiteAsyncConnection _dbConnection;

        public SqlightServiceClass()
        {
            CreatSqlightConnection();
        }
        public async void CreatSqlightConnection()
        {

            if (_dbConnection == null)
            {
                string path1 = FileSystem.Current.AppDataDirectory;
                string dbpath = Path.Combine(path1, "Employees.db3");

                _dbConnection = new SQLiteAsyncConnection(dbpath);

                await _dbConnection.CreateTableAsync<ClientInfoModel>();
            }

        }
6
  • 2
    LocalApplicationData is a path in the WIndows OS. It might not be available on the OS you are using. Commented Jun 2, 2023 at 10:10
  • May I ask if you still meet this issue? If so, what platform or API level you used? Commented Jun 13, 2023 at 2:49
  • There is a similar issue on Github : sqlite path null. You could have a look Commented Jun 15, 2023 at 3:17
  • the platform is android API 33 Commented Jun 16, 2023 at 13:33
  • Does Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) return null? Commented Jun 22, 2023 at 1:33

1 Answer 1

2

Instead of using Environment, you could use the File System Helpers from MAUI Essentials:

string dbpath = Path.Combine(
    FileSystem.Current.AppDataDirectory,
    "clientdb.db3");

It will provide the private application data directory on each platform.

In order to see what the paths are, you can either put a breakpoint after the statement that combines the path or output the directory part of the path to the console:

Console.WriteLine(FileSystem.Current.AppDataDirectory);

The Path.Combine() method will throw an ArgumentNullException if the path is actually null, as per the documentation.

Sign up to request clarification or add additional context in comments.

7 Comments

does not work, with no error or exception but the path is still null
Can you put a breakpoint on the line where you create the string and inspect the AppDataDirectory property? It shouldn't be null, because then you'd see an ArgumentNullException as per the documentation of the Path.Combine() method.
yes, it gives an exception but not every time I run the app, many times I run the app without any exception, and without data being displayed when it throws an exception it says that it's a user exception, not an error
Then please show more of the code. The problem may be occurring somewhere else.
@HossamTash: What do you mean by user exception?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.