Solved

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

Posted on 2010-11-23
7
424 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

815 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now