How to copy a local DB in Xamarin.Forms?

Federico Navarrete 621 Reputation points
2021-06-24T05:34:06.233+00:00

I created a project where I'm trying to copy to local files in Xamarin.Forms, one is an XML and one is a local DB in SQLite:

108760-screen-shot-2021-06-24-at-072123.png

I configured them as EmbeddedResources. I tried to access their current location using this:

public static class Constants  
{  
        public const string DatabaseFilename = "savethatpower.db";  
  
        public const SQLite.SQLiteOpenFlags Flags =  
            // open the database in read/write mode  
            SQLite.SQLiteOpenFlags.ReadWrite |  
            // create the database if it doesn't exist  
            SQLite.SQLiteOpenFlags.Create |  
            // enable multi-threaded database access  
            SQLite.SQLiteOpenFlags.SharedCache;  
  
        public static string DatabasePath  
        {  
            get  
            {  
                var basePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);  
                return Path.Combine(basePath, DatabaseFilename);  
            }  
        }  
}  
  
public void CopyFiles(string name) {  
  
    var localPath = $"{Application.Current.Resources.Source.AbsolutePath}/{name}";  
  
    File.Copy(localPath, Constants.DatabasePath);  
}  
  
CopyFiles("savethatpower.db");  
CopyFiles("dbInfo.xml");  

This line provides me a tricky bug since the beginning: var localPath = $"{Application.Current.Resources.Source.AbsolutePath}/{name}";

InnerException	{System.NullReferenceException: Object reference not set to an instance of an object   at SaveThatPower.Classes.XML.XML2CSharp..ctor (System.String name) [0x00008] in /Users/fanmixco/Documents/GitHub/SaveThatPower/SaveThatPower/SaveThatPower/Classes/XML/XML2CSh…}	System.NullReferenceException  

I found that the Source is null but I don't know how to access its current location. I also tried directly accessing the files like this:

    File.Copy("dbVersion.xml", Constants.DatabasePath);  

But nothing happened. I'm wondering, do you know what is the correct way to copy files? Thanks.

Developer technologies | .NET | Xamarin
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Federico Navarrete 621 Reputation points
    2021-06-24T08:15:49.437+00:00

    This is my solution:

    public class FileActions
    {
        public async Task CopyFile(string name)
        {
            var basePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            var finalPath = Path.Combine(basePath, name);
    
            if (File.Exists(finalPath))
            {
                File.Delete(finalPath);
            }
    
            var assembly = GetType().Assembly;
            var tmpName = $"{assembly.GetName().Name}.{name}";
            using var tempFileStream = assembly.GetManifestResourceStream(tmpName);
            using var fileStream = File.Open(finalPath, FileMode.CreateNew);
            await tempFileStream.CopyToAsync(fileStream);
        }
    }
    

    It is based on Cheesebaron's one.

    0 comments No comments

0 additional answers

Sort by: Most helpful

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.