首页 > 解决方案 > 将 sqlite3 数据库复制到用户文件夹并加载数据库导致应用程序在 WACK 验证期间无法启动

问题描述

当用户第一次运行应用程序时,我的应用程序将 sqlite3 数据库复制到用户文件夹

using System;
using System.Collections;
using SQLitePCL;

//....

public sealed partial class MainPage  
{


    SQLiteConnection db;

    private void onLoaded(object sender, RoutedEventArgs e)
    {
        createEditableCopyOfDatabaseIfNeeded();//remove this line
    }

    public async void createEditableCopyOfDatabaseIfNeeded()
    {
        StorageFolder aFolder = await StorageFolder.GetFolderFromPathAsync(ApplicationData.Current.LocalFolder.Path);

        if (await aFolder.TryGetItemAsync("app.data") != null)
        {
            db = new SQLiteConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, "app.data"));
        }
        else  
        {
            var afile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/app.data"));

            if (afile != null)
            {
                try
                {
                    if(await afile.CopyAsync(ApplicationData.Current.LocalFolder, "app.data") != null)
                    {
                        db = new SQLiteConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, "app.data"));
                    }
                }
                catch (Exception)
                {
                } 
            }
        }
    }
}

此代码适用于我的 PC,但如果我使用 WACK 验证,则报告无法启动应用程序。

如果我删除线

createEditableCopyOfDatabaseIfNeeded();

该应用程序可以通过 WACK 并启动,但无法正常工作(数据库未打开)。

后来,我发现

if(await afile.CopyAsync(ApplicationData.Current.LocalFolder, "app.data") != null)

导致应用程序无法启动,即使我将“app.data”更改为一个小尺寸的 png 文件,它仍然无法正常工作,这意味着该功能

CopyAsync导致应用程序无法在 WACK 中启动

-------------------------------------------------WACK error report
failed Platform version launch
Cannot launch App
The app  failed platform version launch test.
failed  App launch
Cannot launch App
failed Crashes and hangs
Executable myapp.exe was detected by Windows Error Reporting and experienced a crash or hang.
Application  was detected by Windows Error Reporting and experienced a crash or hang.
Crash dump file myapp.exe.1016.dmp was created by Windows Error Reporting and provides additional information.
Crash dump file myapp.exe.12440.dmp was created by Windows Error Reporting and provides additional information.

----------------------------------------------

欢迎您的意见

标签: c#uwp

解决方案


推荐阅读