10

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!

Ghasem
  • 14,455
  • 21
  • 138
  • 171
Blessy Khauhelo
  • 125
  • 1
  • 3
  • 11
  • 2
    Please provide more information. What IDE are you using? What type of project is this? How did you include the text file in the resource folder? What build options did you set on the text file? – Richard Cook Aug 07 '13 at 16:32
  • Im using c#. its a project that has so many text files in it and the way i read these files,is that i open a file and read only the potion of that file i want. this means i sometimes skip lines inside that file depending on what i want. i have included those files(69 text files) in resources directory. so after deployment,i want when a install the setup in another computer,my programme to be able to read those files! im really struggling! i want a full code for that. – Blessy Khauhelo Aug 07 '13 at 16:48
  • You're writing in C#, but telling us which IDE you're using, (visual studio would be my first bet, if not monoDevelop) would help us tell you how to configure your build actions. – Timothy Groote Aug 07 '13 at 17:05

3 Answers3

22

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.

Simon P Stevens
  • 27,303
  • 5
  • 81
  • 107
2

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.

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
1

create this function to read whatever embedded resource text file you have :

public string GetFromResources(string resourceName)
{
    Assembly assem = this.GetType().Assembly;

    using (Stream stream = assem.GetManifestResourceStream(resourceName))
    {
        using (var reader = new StreamReader(stream))
        {
            return reader.ReadToEnd();
        }

    }
}
terrybozzio
  • 4,424
  • 1
  • 19
  • 25
  • How is this not just a copy of what was already written 7 minutes prior? – Timothy Shields Aug 07 '13 at 16:45
  • didnt even see your post timothy(i guess you said this because of your post),i am creating a method for the OP retrieve any text file in resources,as this is not copy of your,i am sorry if i ofended you or anyone else it is not my desire to do so,this was not a copy. – terrybozzio Aug 07 '13 at 16:51
  • 1
    Don't worry about it. Nothing wrong as long as it wasn't intentional. – Timothy Shields Aug 07 '13 at 17:00
  • this one is also very good please check it out.....hummmm even the names of the 2 var are the same...... - http://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file - check the accepted answer,if you probe the answers you will see... – terrybozzio Aug 07 '13 at 17:00