eatumup
eatumup

Reputation: 525

Referencing Text file Visual Studio C#

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?

Upvotes: 4

Views: 8853

Answers (3)

Chuck Rostance
Chuck Rostance

Reputation: 7104

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.

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

Upvotes: 8

Stafford Williams
Stafford Williams

Reputation: 9806

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

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

Upvotes: 2

Paul Sasik
Paul Sasik

Reputation: 81459

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:

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

Upvotes: 6

Related Questions