引用文本文件Visual Studio C# [英] Referencing Text file Visual Studio C#

查看:151
本文介绍了引用文本文件Visual Studio C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦试图硬编码文本文档到我的C#窗体应用程序。基本上我有,我想填充到一个文本框的网页的HTML。我可以很容易地流阅读器做到这一点还是花永远其写入到一个字符串。在不同的元素,我可以添加到我的解决方案放眼望去是一个文本文件。

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.

我想这个编码到exe文件作为默认文档。我不想一个外部文件,以便它不能被编辑和使程序变得易于携带。

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?

推荐答案

右键点击您的项目,然后单击属性。你平时的屏幕有很多左侧选项卡。点击上面写着资源的人。您可以添加字符串和其他的东西,但你也可以添加文本文件。

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.

您可以在.NET网站做到这一点,MVC的网站,控制台应用程序等等。下面的代码是控制台应用程序的风格保持简单。当然,这仅仅是一个文本文件,但它可以让你使用资源的基本知识。你可以扩展为其他类型的文件,包括3部分的东西,如报告模板,格式文件,XML和其他的东西,否则可能会笨拙总是试图确保当你提升你的代码包含在你的配置。 。缺点是,你必须重新编译进行修改

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.

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
        }
    }
}

这篇关于引用文本文件Visual Studio C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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