有没有一种方法可以获取对嵌入式资源文件的字符串路径引用? [英] Is there a way to get a string path reference to an embedded resource file?

查看:76
本文介绍了有没有一种方法可以获取对嵌入式资源文件的字符串路径引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个采用字符串参数的方法,该参数是文本文件的文件路径。我希望能够传递作为资源嵌入到程序集中的文本文件。

I have a method that takes a string parameter that is a file path to a text file. I want to be able to pass in a text file that I have embedded as a resource in my assembly.

有什么方法可以获取对嵌入式文本文件的字符串引用,以便将其用作打开StreamReader的文件路径?

Is there any way to get a string reference to an embedded text file so that it would function as a file path for opening a StreamReader?

谢谢。

推荐答案

您可以使用 Assembly.GetManifestResourceStream(resource_name_of_the_file) 访问文件的流,将其写入TEMP目录并使用此路径。

You can use Assembly.GetManifestResourceStream(resource_name_of_the_file) to access the file's stream, write it to TEMP directory and use this path.

例如,如果您的项目中有一个文件,路径 Resources\Files\File.txt 并且项目的程序集默认名称空间为 RootNamespace ,则可以使用以下程序集从此程序集代码中访问文件流

For example, if you have a file in your project at the path "Resources\Files\File.txt" and the project's assembly default namespace is "RootNamespace", you can access the file's stream from within this assembly's code with

Assembly.GetExecutingAssembly().GetManifestResourceStream("RootNamespace.Resources.Files.File.txt")

这篇关于有没有一种方法可以获取对嵌入式资源文件的字符串路径引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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