DanyDC
DanyDC

Reputation: 73

How read and write in an embedded text file?

When the program starts, I want that it can read an embedded text file in the resources folder of the project and show the content of it in a textbox for example. And when I click a button on my form, I can write in this text file.

I already tested several things :

string text = File.ReadAllText(Properties.Resources.favoris); //"favoris" is a text file.

But I have an error because Properties.Resources.favoris is not a file path.

I want the more simple way to do that.

Upvotes: 0

Views: 821

Answers (1)

Jeff Lequeux
Jeff Lequeux

Reputation: 276

To read the embedded test, you should use:

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "namespaceOfAssembly.Folder.MyFile.txt";

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
    string result = reader.ReadToEnd();
}

To write in a embedded file, it is not possible since an embedded resources are compiled into your assembly.

Upvotes: 1

Related Questions