引用文本文件 Visual Studio C#
我在尝试将文本文档硬编码到我的 C# 表单应用程序中时遇到问题。基本上我有网页 HTML,我想将其填充到文本框中。我可以使用流阅读器轻松地完成此操作,或者花费大量时间将其写入字符串中。环顾四周,我可以添加到解决方案中的不同元素是一个文本文件。
因此,我正在寻找一种简单的方法来告诉我的应用程序读取文本文件或类似的内容并将其放入文本框中。到目前为止,我不知道如何引用我添加到表单中的文本文件。
我希望将其作为默认文档编码到 exe 中。我不需要外部文件,这样就无法对其进行编辑,从而使程序变得易于移植。
关于如何内部嵌入此文件并像类或其他东西一样访问它有什么建议吗?
I am having trouble trying to hard encode a text document into my C# forms application. Basically I have webpage HTML that I want to populate into a text box. I can easily do this with stream reader or spend forever writing it into a string. Looking around at the different element I can add to my solution was a textfile.
So I am looking for an easy way to tell my application to read a text file or something similar and put it in the text box. So far I can't figure out how to reference the text file I added to the form.
I want this coded into the exe as a default document. I don't want an external file so that it cant be edited and so that the program becomes easily portable.
Any suggestions on how to internally embed this file and access it like a class or something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
右键单击您的项目,然后单击属性。您会看到通常的屏幕,左侧有很多选项卡。单击标有“资源”的那个。您可以添加字符串和其他内容,但也可以添加文本文件。
将顶部的下拉列表更改为文件,然后单击“添加资源”。浏览到一个文本文件,随意命名。这会将文件添加到您的程序集中。不要太兴奋,因为它可以被反编译,所以不要立即存储任何秘密消息。
您可以在 .net 站点、mvc 站点、控制台应用程序等中执行此操作。下面的代码是控制台应用程序样式,以保持简单。当然,这只是一个文本文件,但它为您提供了使用资源的基础知识。您可以将其扩展到其他类型的文件,包括第三部分的内容,例如报告模板、格式文件、xml 以及其他内容,否则在升级代码时始终尝试确保将其包含在您的配置中可能会很笨拙。缺点是,您必须重新编译才能进行更改。
Right click on your project and click properties. You get the usual screen with lots of tabs on the left. Click the one that says "Resources". You can add strings and other things, but you can also add text files.
Change the dropdown at the top to files, then click "Add Resource." Browse to a text file, name it whatever. This will add the file into your assembly. Don't get too excited because this can be decompiled so don't store any secret messages anon.
You can do this in a .net site, mvc site, console app, etc. The code below is console app style to keep it simple. Granted, this is just a text file, but it gives you the basics of using resources. You could expand this to other types of files, including 3rd part stuff like report templates, format files, xml, and other things that otherwise might be clumsy to always try ensure are included in your config when you promote your code. Downside, you have to recompile to make changes.
您描述的概念称为嵌入资源。
您可以嵌入各种文件作为资源;图标、图像、xml 甚至纯文本文件,并从代码中引用它们,例如:
当您知道要查找的内容时,可以使用大量文档,如下所示: http://msdn.microsoft.com/en-us/library/0c6xyb66.aspx
The concept you're describing is called embedding resources.
You can embed all sorts of files as resources; icons, images, xml and even plain text files and the reference them from code, like this for example:
There's tons of documentation available when you know what you're looking for like this: http://msdn.microsoft.com/en-us/library/0c6xyb66.aspx
我做了类似的事情。
将 .html 添加到您的项目中,将属性中的构建操作设置为嵌入式资源
然后,您可以通过以下方式获取该项目的流:
I do something similar.
Add the .html to your project, set Build Action in properties to Embedded Resource
Then, you can get a stream to that item via