Solved

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

Posted on 2010-11-23
7
428 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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