This question has already been answered:

How to open this test.txt file from the program? For example, when processing a button press. Resource files

Marked as a duplicate by MSDN.WhiteKnight , nick_n_a , aleksandr barakin , br3t , 0xdb December 14, '18 at 17:40 .

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 .

    2 answers 2

    Change the file property File properties

    Then you can:

    var assembly = Assembly.GetExecutingAssembly(); var resourceName = assembly.GetName().Name + ".Resources.test.txt"; using (Stream stream = assembly.GetManifestResourceStream(resourceName)) using (StreamReader reader = new StreamReader(stream)) { string result = reader.ReadToEnd(); Console.Write(result); } Console.ReadLine(); 

    But there is another option: change the properties of Copy to Output Directory to Copy always (I don’t know how it will be in Russian)

    File file

    Then you can just

    StreamReader streamReader = new StreamReader("Resources\\test.txt");

    • prntscr.com/luuhsi I did everything, changed the Build Action property I need But the file does not open. After launch, the program simply closes - Andrei Lyovushkin
    • @AndreyLёvushkin, this is how it should be. If you want to display something, use Console.WriteLine and then Console.Readline to prevent the window from closing. Look I changed the code - DIlshod
    • And if we have an executable file, and need to execute it from the resources of the program? - Andrei Lyovushkin
    • @AndreyLevushkin, If you meant the execution of commands via the Windows command line (.bat, .exe files), then in C # you can execute them without storing any files. See stackoverflow.com/questions/5519328/… - DIlshod

    Use the GetManifestResourceStream method.

    For example:

     Assembly assembly = Assembly.GetExecutingAssembly(); using (Stream stream = assembly.GetManifestResourceStream("имя_файла")) using (StreamReader reader = new StreamReader(stream)) { string data = reader.ReadToEnd(); } 
    • Error: prntscr.com/lutndd - Andrey Levushkin
    • one
      @AndreyLёvushkin you have a file in a subfolder, the name will most likely be Resources.test.txt and yes, the file should be marked as embeded in the properties. - Monk
    • I can not find for some reason, where in the properties of the file mark it as embeded - Andrey Lyovushkin