如何处理应用程序资源 [英] How to deal with application resources

查看:83
本文介绍了如何处理应用程序资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#和WPF ...我在问这个问题,因为我从未找到解决我以前的问题的方法!!所以我想将一个空的访问数据库文件添加到应用程序资源并将其复制到特定位置,我想知道如何从资源中添加/检索文件...此外,还有一种方法可以更新文件在应用程序运行时(例如备份),以便在将应用程序移动到另一个位置或PC时使用最新更新)?

I`m using C# and WPF ... I`m asking this question as I have never found a solution to my previous questions!! so I want to add an empty access database file to the application resources and to copy it to a specific location, I want to know how to add/retrieve the file from the resources ... also, is there a way to update the file during the runtime of the application (like a backup, so that if I move the app to another location or pc the latest updates are used)??

推荐答案

using (var resourceStream = Assembly
    .GetExecutingAssembly()
    .GetManifestResourceStream(resourceName))
{
    if (resourceStream != null)
    {
        //read the stream

棘手的问题位使resourceName正确。您需要将文件位置转换为它所属的名称空间。

The tricky bit is getting the resourceName correct. You need to convert the file location into the namespace it belongs to.

因此,例如,默认名称空间为 DefaultNS ,并将文件保存在名为 resources 的项目文件夹中,文件名为 myfile.ext ,资源名称:

So for instance, with a default namespace of DefaultNS, and the file living in a project folder called resources with a filename of myfile.ext, you would have a resource name of:

DefaultNS.resources.myfile.ext

这篇关于如何处理应用程序资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆