Solved

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

Posted on 2010-11-23
7
414 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

21 Experts available now in Live!

Get 1:1 Help Now