如何将外部文本文件作为资源添加到项目中

发布于 2024-08-26 05:28:57 字数 186 浏览 3 评论 0原文

我正在 c#.net 中开发一个 consol 应用程序,在此我需要一个驻留在本地磁盘中的文本文件,但我想使其动态并将该文件作为资源添加到项目中,因此当我创建项目的最终 exe 并放置它可以在任何地方正常工作,而无需在本地磁盘上放置该文件。

请帮助我解决这个问题

并详细描述这个问题,因为我也是这个框架和控制台应用程序的新手。

i m devlopuing one consol application in c#.net and in this i need one text file which is resides in local disc but i wanna make it dynamic and add that file in the project as a resources so when i create final exe of my project and place it any where it works proparly without having that file in local disc.

pls help me regarding this issue

and describe this in detail because i m new with this framwork and consol applocation too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚气少女 2024-09-02 05:28:57

您可以将文本文件作为资源嵌入到可执行文件中,然后在运行时将其作为流获取。将文件添加到您的项目并将“构建操作”更改为“嵌入式资源”。

然后,运行时获取资源:

Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream("Namespace.filename.txt");

特别注意资源流名称中的 Namespace 部分,即您添加文件的项目文件夹的命名空间。然后它将附加实际的文件名,包括后缀。

You can embed the text file as a resource into the executable and then get it as a stream when running. Add the file to your project and change "Build Action" to "Embedded Resource".

Then, to get the resource when running:

Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream("Namespace.filename.txt");

Especially note the Namespace part of the resource stream name, that is the namespace of the project folder where you added the file. It will then be appended with the actual filename, including suffix.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文