Solved

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

Posted on 2010-11-23
7
434 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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