C#: Embedded files

Posted on 2009-12-17
Last Modified: 2013-12-17
I have been racking my brain to solve this problem. I have two C# application.

1: CSharpUtilityBelt (Class Library built into a DLL)
2: ConsoleApplication1 (Built into an EXE)

In the class library I have added a XML file and set it to embedded resource. Then in ConsoleApplication1 I referenced CSharpUtilityBelt. I have been looking for ways to get to the embedded resource, here is what I found.

"Do you want the data in the file or the filename? I use the following to
access the contents of an embedded file:

Stream stream =
Assembly.GetExecutingAssembly().GetManifestResourc eStream(name);

Note that the name needs to include the default namespace for the[/color][/color]
e.g. If the default namespace was My.Namespace and the file with its[/color][/color]
action set to "Embedded Resource" was called TextFile1.txt then you'd[/color][/color]
to pass "My.Namespace.TextFile1.txt" to GetManifestResourceStream().

You can check what the name to use for a built assembly by loading the
assembly into ildasm.exe, opening the manifest, and looking for the[/color]
.mresource entry.


 .mresource public My.Namespace.TextFile1.txt

Here is my code in ConsoleApplication1:

System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(ConsoleApplication1.TestXMLFile.xml);

I tried the namespace ConsoleApplication1.TestXMLFile.xml and CSharpUtilityBelt.TestXMLFile.xml. Eitehr way I get the error "The type or namespace name 'TestXMLFile' does not exist in the namespace 'ConsoleApplication1' (are you missing an assembly reference?)". I do not know what to do next.
Question by:Mister_Spock
    LVL 10

    Expert Comment

    Since the XML file is embedded in de dll, I think you need to get it from within the dll as well.
    You say you do it from your ConsoleApplication1, so GetExecutingAssembly will return the assembly for the Console Application and not the dll-assembly.

    Can you try to move that code to a function in the dll, and then call that function from the Console application?
    LVL 3

    Author Comment

    How do I get the DLL Assembly though, I've tried both namespaces with no luck.
    LVL 10

    Accepted Solution

    You should move the code

    System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("CSharpUtilityBelt.TestXMLFile.xml");

    to a function inside the dll.  
    Then GetExecutingAssembly should return the DLL assembly (since that one is executing at that moment).
    Note that the name of the manifest resource is case sensitive.
    LVL 3

    Author Closing Comment

    500 points is not enough....thank you very much.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

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

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 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

    11 Experts available now in Live!

    Get 1:1 Help Now