Resources 资源文件命名

本文介绍了如何在MauiApp项目中管理资源文件,包括添加资源、将其设置为嵌入资源以及通过资源名称访问数据库文件的操作。作者详细展示了如何使用`Assembly.GetManifestResourceStream`加载和复制资源到本地路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、添加资源文件

项目,MauiApp2.Library属性,资源,常规,创建或打开程序集资源,添加资源,添加现有文件,poetrydb.sqlite

2、嵌入的资源

选中资源文件,按F4打开属性,生成操作,改为 嵌入的资源

在这里插入图片描述

3、修改资源名称
<ItemGroup>
    <EmbeddedResource Include="Resources\poetrydb.sqlite" >
        <LogicalName>poetrydb.sqlite</LogicalName>
    </EmbeddedResource>
</ItemGroup>

在这里插入图片描述

4、通过资源名直接调用
/// <summary>
/// 数据库文件存储位置
/// </summary>
public const string DbName = "poetrydb.sqlite";
public static readonly string PoetryDbPath =
    Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), DbName);

/// <summary>
/// 初始化或更新数据库版本
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task InitializeAsync()
{
    // 打开写入的文件
    await using var dbFileStream = new FileStream(PoetryDbPath, FileMode.OpenOrCreate);

    // 打开读取的文件
    await using var dbAssetStream = typeof(PoetryStorage).Assembly.GetManifestResourceStream(DbName);

    // 复制并写入文件
    await dbAssetStream.CopyToAsync(dbFileStream);

    // 关闭读取的文件

    // 关闭写入的文件

    // 写入新的版本号
    _preferenceStorage.Set(PoetryStorageConstant.DbVersionKey, PoetryStorageConstant.Version);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值