Access denied error on copy resource file to device

Hello, I'm new in this forum.
I'm trying to embed existing sqlite database to my application (using (visual studio 2017, on windows 10)) but always got "Access denied" error, even when I tried it in my local machine (development PC, not real phone device). Here are my code snippet :

    string Fname = "data.sqlite";
    string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    string dbPath = Path.Combine(folderPath, Fname);
    string ds = "Data Source=" + dbPath;
    //Environment.GetFolderPath (Environment.SpecialFolder.Personal)


    if (File.Exists(dbPath))
    {
        db = new SQLiteConnection(ds);
        db.Open();
    }
    else
    {
        using (Stream source = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(Fname))
        {
            using (var destination = File.Create(folderPath))
            {
                source.CopyTo(destination);
            }
        }
    }

It stuck in the File.Create(folderPath) with "Access denied" error. I've tried to change target folder to other location : System.Environment.SpecialFolder.Personal, System.Environment.SpecialFolder.System.Environment.SpecialFolder.LocalApplicationData, System.Environment.SpecialFolder.MyDocuments but the problem persist.

What am I doing wrong here ?. What should I do to fix those problem ?.

Thanks in advance for your help

Best Answer

  • sholikhinsholikhin ✭✭
    Accepted Answer

    Accessing the database directly from the path work just fine on local machine / uwp emulator, the apps shows the data from database as expected.

            string Fname = "data.sqlite";
            string folderPath = System.AppContext.BaseDirectory;
            string dbPath = Path.Combine(folderPath, "Assets");
            dbPath = Path.Combine(dbPath, Fname);
            string ds = "Data Source=" + dbPath;
    
            db = new SQLiteConnection(ds);
            db.Open();
    

    I just hope that the directory structure is identical on windows phone.

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Use dbPath instead of folderPath.

    File.Create only accepts file path not folder path.

  • sholikhinsholikhin Member ✭✭

    Or is it caused by the targ> @ColeX said:

    Use dbPath instead of folderPath.

    File.Create only accepts file path not folder path.

    Thanks for your reply, but the problem persist. Got access denied

  • sholikhinsholikhin Member ✭✭

    @ColeX said:
    Use dbPath instead of folderPath.

    File.Create only accepts file path not folder path.

    >

    Still got access denied for Environment.SpecialFolder.Personal & Environment.SpecialFolder.MyDocument but successful for Environment.SpecialFolder.LocalApplicationData. But file size is 0 bytes, new files (not my existing sqlite database, 14 mb).

  • sholikhinsholikhin Member ✭✭

    @sholikhin said:

    @ColeX said:
    Use dbPath instead of folderPath.

    File.Create only accepts file path not folder path.

    >

    Still got access denied for Environment.SpecialFolder.Personal & Environment.SpecialFolder.MyDocument but successful for Environment.SpecialFolder.LocalApplicationData. But file size is 0 bytes, new files (not my existing sqlite database, 14 mb).

    Just realized that it's another error : System.NullReferenceException: Object reference not set to an instance of an object.
    Is there any way to verify that the file is included in the resource ?. I have copied the file to assets in the visual studio project and set build properties to "Embedded resource" before the build.

  • sholikhinsholikhin Member ✭✭

    @sholikhin said:

    @sholikhin said:

    @ColeX said:
    Use dbPath instead of folderPath.

    File.Create only accepts file path not folder path.

    >

    Still got access denied for Environment.SpecialFolder.Personal & Environment.SpecialFolder.MyDocument but successful for Environment.SpecialFolder.LocalApplicationData. But file size is 0 bytes, new files (not my existing sqlite database, 14 mb).

    Just realized that it's another error : System.NullReferenceException: Object reference not set to an instance of an object.
    Is there any way to verify that the file is included in the resource ?. I have copied the file to assets in the visual studio project and set build properties to "Embedded resource" before the build.

    Been trying to set Fname to "data.sqlite", "namespace.data.sqlite", "namespace.resources.data.sqlite" but the problem persist

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    What platform is this on? For iOS you should put the source sqlite database in Resources, for Android you should put it in Assets. Then copying from each location is platform-specific. You should not need to use reflection and manfests etc. to get at the files. Or is this on UWP?

  • sholikhinsholikhin Member ✭✭

    @JamesLavery said:
    What platform is this on? For iOS you should put the source sqlite database in Resources, for Android you should put it in Assets. Then copying from each location is platform-specific. You should not need to use reflection and manfests etc. to get at the files. Or is this on UWP?

    It's multiplatform (Android, iOS, UWP), but what i've posted above is based on testing the UWP.

  • sholikhinsholikhin Member ✭✭

    @JamesLavery said:
    What platform is this on? For iOS you should put the source sqlite database in Resources, for Android you should put it in Assets. Then copying from each location is platform-specific. You should not need to use reflection and manfests etc. to get at the files. Or is this on UWP?

    Actually I only read only access to the sqlite database, no update. Prefer not to copy anything, but I read somewhere that we have copy it to other folder in order to use the database. Or, is it ok to just use the database directly from ./appx/assets/data.sqlite ?. I have checked that the data is in there

  • sholikhinsholikhin Member ✭✭
    Accepted Answer

    Accessing the database directly from the path work just fine on local machine / uwp emulator, the apps shows the data from database as expected.

            string Fname = "data.sqlite";
            string folderPath = System.AppContext.BaseDirectory;
            string dbPath = Path.Combine(folderPath, "Assets");
            dbPath = Path.Combine(dbPath, Fname);
            string ds = "Data Source=" + dbPath;
    
            db = new SQLiteConnection(ds);
            db.Open();
    

    I just hope that the directory structure is identical on windows phone.

Sign In or Register to comment.