简体   繁体   English

引用文本文件Visual Studio C#

[英]Referencing Text file Visual Studio C#

I am having trouble trying to hard encode a text document into my C# forms application. 我无法将文本文档硬编码到我的C#表单应用程序中。 Basically I have webpage HTML that I want to populate into a text box. 基本上我有网页HTML,我想填充到文本框中。 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. 我想将此编码作为默认文档编写到exe中。 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? 有关如何内部嵌入此文件并像类或类似的方式访问它的任何建议?

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. 您可以在.net站点,mvc站点,控制台应用程序等中执行此操作。下面的代码是控制台应用程序样式,以保持简单。 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. 您可以将其扩展为其他类型的文件,包括报表模板,格式文件,xml等第三部分内容,以及在您宣传代码时,始终尝试确保的其他内容可能会包含在您的配置中。 Downside, you have to recompile to make changes. 在下侧,您必须重新编译才能进行更改。

using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var output = Properties.Resources.helloword;
            Console.WriteLine(output);
            Console.ReadLine(); //just so you can see it
        }
    }
}

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: 图标,图像,xml甚至纯文本文件以及从代码中引用它们,例如:

internal string GetFromResources(string resourceName)
{
    Assembly assem = this.GetType().Assembly;
    using (Stream stream = assem.GetManifestResourceStream(resourceName))
    {
        using (var reader = new StreamReader(stream))
        {
            return reader.ReadToEnd();
        }
    }
}

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 当你知道你正在寻找的东西时,有大量的文档可用: http//msdn.microsoft.com/en-us/library/0c6xyb66.aspx

I do something similar. 我做了类似的事情。

Add the .html to your project, set Build Action in properties to Embedded Resource 将.html添加到项目中,将属性中的Build Action设置为Embedded Resource

Then, you can get a stream to that item via 然后,您可以通过此流程获取该项目的流

Assembly.GetExecutingAssembly().GetManifestResourceStream("your.namespace.yourFile.html");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM