This question has already been answered:

I want to add several csv files to the project and use them as text resources. Well, to lay together in a folder for order. Create a folder no problem.

project context menuAddCreate folder

Files are also created

folder context menuAddCreate item Visual Visual C # elementsText file

But how to use their contents?

Reported as a duplicate by participants 4per , MihailPw , αλεχολυτ , pavel163 , Vadim Ovchinnikov Jun 14 '17 at 20:35 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    Um ... Do you propose to close as a duplicate of your own question? o_O And I did not understand what have the resources. - Qwertiy
  • @Qwertiy, yes, your own. Found that there is a duplicate. And with the resources already everything is in order, the answers are here and there. - 4per

2 answers 2

https://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file

After adding a resource, you need to set in the designer the property Действия при сборке value of the Внедренный ресурс (naming in Russified Visual Studio, in English language will be Build Action and Embedded Resource ). To extract a text file, you will need to use the GetManifestResourceStream() method of the Assembly class, an instance of which the corresponding executable assembly can be obtained by the static Assembly.GetExecutingAssembly() method. Do not forget that to use Assembly you must enter using System.Reflection in the module header or refer to it System.Reflection.Assembly .

GetManifestResourceStream() returns a Stream . Knowing that we are dealing with text, either we read the System.IO.StreamReader entire file into an instance of string .

 using System.Reflection; using System.IO; ... public static string GetTextResource(string @namespace, string folder, string filename) { using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( $"{@namespace}.{folder}.{filename}")) { using (StreamReader sr = new StreamReader(stream)) { return sr.ReadToEnd(); } } } 

Or we will organize other logic of reading the file.

    Use the resource mechanism.
    Go to the project properties, the "Resources" tab, add a new resource file (if you don’t have one yet), add a new or existing text file.
    After that, you can refer to this file as Properties.Resources.<Имя файла> .
    Those. if you added the main.csv file, you can access its contents as Properties.Resources.main . This will be a read-only line with the contents of the file.

    • I want a folder. Dump all the resources in the project into one heap of Resources, it is easy and convenient for the time being - 4per