A
A
andrey_levushkin2018-12-05 15:50:35
C++ / C#
andrey_levushkin, 2018-12-05 15:50:35

How to open a file from a VS project using C#?

There is a small piece of software. It has a button. By clicking on the button, the 1.txt file should open, which will initially already be in the project resources. That is, the file should not be opened from a specific location on the disk, but already inside the program, from its resources.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Satisfied IT, 2018-12-05
@andrey_levushkin

If I understand the question correctly, then something like this.
1 - Add the following using
2 - Set the following property on the desired file:
Parameter Build Action, value Embedded Resource
3 - Use the following code

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "MyCompany.MyProduct.MyFile.txt";

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question