How to deal with application resources

…衆ロ難τιáo~ 提交于 2019-12-25 04:49:09

问题


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)??


回答1:


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

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

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



回答2:


Adding and Editing Resources (Visual C#)




回答3:


You can do that in Visual Studio, when you click on the file you want as a resource, go to Properties window and change "Build action" to Resource. I this window, there is also the option to copy the resource to the target directory.



来源:https://stackoverflow.com/questions/3378491/how-to-deal-with-application-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!