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.