0

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.

DanyDC
  • 73
  • 1
  • 14

1 Answers1

1

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.

Jeff Lequeux
  • 276
  • 4
  • 5
  • Can you be more precise ? Where do I put this code in my code ? How do I use it ? – DanyDC Mar 12 '16 at 08:36
  • If I cannot write in the text file, how could I make to have an readable and writable text file in my projects to be able to launch the exe from any computer without having problems of UNC path? – DanyDC Mar 12 '16 at 17:13
  • you can use user properties, with default properties. (https://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx) – Jeff Lequeux Mar 13 '16 at 18:01