Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading an embedded text file

I have created a full project which works perfectly. My problem concerns the setup project. When I use it on another computer, the text file cannot be found even if they are inside the resource folder during the deployment!

How can I ensure that my program will find those text files after installing the software on another computer!

I have been looking for this solution but in vain. Please help me sort this out. If I can get a full code that does that i will be very happy!

like image 653
Botle Khauhelo Avatar asked Aug 07 '13 16:08

Botle Khauhelo


People also ask

How do I read an embedded text file?

To read embedded resource file, we can use Assembly. GetManifestResourceStream API. The following helper class provides two functions: one return a Stream object and another returns the resource file content as a string.

What is embedded file in C#?

Embedded files are called as Embedded Resources and these files can be accessed at runtime using the Assembly class of the System.Reflection namespace. This article will illustrate, how to read and display an embedded Text file and Image file in Windows Application (Windows Forms) in C# and VB.Net. Download Code Sample.

What is an embedded resource?

Embedded resource has no predefined structure: it is just a named blob of bytes. So called “. resx” file is a special kind of embedded resource. It is a string-to-object dictionary that maps a name to an object. The object may be a string, an image, an icon, or a blob of bytes.

How do I add embedded resources to Visual Studio?

Open Solution Explorer add files you want to embed. Right click on the files then click on Properties . In Properties window and change Build Action to Embedded Resource . After that you should write the embedded resources to file in order to be able to run it.


2 Answers

FIrst set the build action of the text file to "EmbeddedResource".

Then to read the file in your code:

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

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

If you can't figure out the name of the embedded resource do this to find the names and it should be obvious which your file is:

assembly.GetManifestResourceNames();

This is assuming you want the text file to be embedded in the assembly. If not, then you might just want to change your setup project to include the text file during the installation.

like image 172
Simon P Stevens Avatar answered Oct 06 '22 01:10

Simon P Stevens


Assuming you mean that you have a file in your project that you've set as an EmbeddedResource, you want

using (var stream = Assembly.GetExecutingAssembly()
    .GetManifestResourceStream(path))
{
    ...
}

where path should be the assembly name followed by the relative path to your file in the project folder hierarchy. The separator character used is the period ..

So if you have an assembly called MyCompany.MyProject and then in that project you have a folder Test containing Image.jpg, you would use the path MyCompany.MyProject.Test.Image.jpg to get a Stream for it.

like image 28
Timothy Shields Avatar answered Oct 06 '22 01:10

Timothy Shields