Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using an embedded text file in an ASP.NET compiled DLL

Posted on 2010-11-23
7
Medium Priority
?
451 Views
Last Modified: 2012-05-10
I would like to use embedded text files in a .NET DLL I am building. I have added the text files in a subfolder of the project and added the relevant code to AssemblyInfo.vb.  The subfolders are residing in the MyProject > Templates > Text folder.

I need to read in the embedded text files (much like a web app would read in a normal .txt file) but I am unsure as to how to do this.  The namespace/filename I am using to access the file is MyProject.Templates.Text.TemplateFile.txt where MyProject is the root namespace.

The code I have is as follows, but I am getting an "Object reference not set to an instance of an object" error.

 
Protected Function LoadEmbeddedTemplate(ByVal EmbeddedFilename As String) As String

'NB - the EmbeddedFilename variable is passed as "MyProject.Templates.Text.TemplateFile.txt"

            Dim f As Stream = System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream(EmbeddedFilename)

            Dim st As New StreamReader(f)
            st.ReadToEnd()
            st.Close()

            Return st.ToString

        End Function

Open in new window

0
Comment
Question by:pipelineconsulting
  • 4
  • 2
7 Comments
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 34196023
0
 

Author Comment

by:pipelineconsulting
ID: 34196209
Thanks for this.

The article isn't quite clear on how files that are in subfolders should be referenced.  

Should they be of the form:

"DefaultNamespace.EmbeddedFilename.txt"

or

"DefaultNamespace.Folder1.Folder2.Folder3.EmbeddedFilename.txt"

where Folder1.Folder2.Folder3 is the "path" to the filename in the solution?

It also doesn't demonstrate how to read in the embedded file in the DLL itself (e.g. using a stream reader as above) - the examples are just rendering to a .ASPX page.

0
 
LVL 3

Expert Comment

by:saifikram_md
ID: 34196374
It should be
Assembly _assembly = Assembly.GetExecutingAssembly();
StreamReader _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNameSpace.MyTextFile.txt"));
if(_textStreamReader.Peek() != -1)
      {
         textBox1.Text = _textStreamReader.ReadLine();
      }

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:pipelineconsulting
ID: 34196887
This code does not work so far. The error thrown is:

Value cannot be null.
Parameter name: stream


I assume this is because the file cannot be loaded because of a filename error.  I just want to load the template text file into a string for now rather than outputting to a textbox.  Can someone confirm what the correct argument should be for

(_assembly.GetManifestResourceStream(argument))

My root namespace is of the form RootNamespace.AppNamespace and the embedded resource is in a folder called "Templates".

My AssemblyInfo.vb contains the lines:

 
<Assembly: System.Web.UI.WebResource("RootNamespace.AppNamespace.TemplateFile.txt", "text/plain")>

Open in new window


Is this correct?

Should the argument be:

1. RootNamespace.AppNamespace.TemplateFile.txt,
2. RootNamespace.AppNamespace.Templates.TemplateFile.txt,
3. Templates.TemplateFile.txt  , or,
4. TemplateFile.txt

Please note I am trying to access the embedded resource from WITHIN the DLL itself - i.e the code is part of a class within the DLL.
0
 
LVL 3

Accepted Solution

by:
saifikram_md earned 2000 total points
ID: 34197730
See this is what i have done.

1. created a class library
2. added a file "TextFile1.txt"
3. modified the properties for the "TextFile1.txt" as embeded resource
4. wrote the code for class1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;

namespace ClassLibrary1
{
    public class Class1
    {
        public string text
        {
            get
            {
                string retData = "";
                Assembly _assembly = Assembly.GetExecutingAssembly();
                StreamReader _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("ClassLibrary1.TextFile1.txt"));
                if (_textStreamReader.Peek() != -1)
                {
                    retData = _textStreamReader.ReadLine();
                }
                return retData;
            }
        }
    }
}

Open in new window


5. Referenced the above class library
6. To retrieve the resource i wrote
ClassLibrary1.Class1 c1 = new ClassLibrary1.Class1();
            MessageBox.Show(c1.text);

Open in new window


Consider your project name is assemblyName (rootnamespace). Now you have created a folder under the root as Templates and then you have added a file TemplateFile.txt

Now assemblyinfo should have the details as:
"!RootNamespace.Templates.TemplateFile.txt"

Resource files do not inherit any other namespace except the location.







0
 

Author Comment

by:pipelineconsulting
ID: 34205601
I've got this working now.

It turns out that in C#, the embedded resource namespace should include the folder path/location.  In VB (for some unknown reason), the embedded resource namepsace is just RootNamspace.Filename.Extension (e.g. MyNamespace.TemplateFile.txt)

0
 

Author Closing Comment

by:pipelineconsulting
ID: 34205605
Thanks very much for your help, much appreciated!
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question