Mister_Spock
asked on
C#: Embedded files
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.GetExecutingAssem bly().GetM anifestRes ourc eStream(name);
Note that the name needs to include the default namespace for the[/color][/color]
project.[color=blue][color =green]
e.g. If the default namespace was My.Namespace and the file with its[/color][/color]
build[color=blue][color=gr een]
action set to "Embedded Resource" was called TextFile1.txt then you'd[/color][/color]
need[color=blue][color=gre en]
to pass "My.Namespace.TextFile1.tx t" 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]
relevant[color=green]
.mresource entry.
e.g.
.mresource public My.Namespace.TextFile1.txt
{
}"
Here is my code in ConsoleApplication1:
System.IO.Stream stream = System.Reflection.Assembly .GetExecut ingAssembl y().GetMan ifestResou rceStream( ConsoleApp lication1. TestXMLFil e.xml);
I tried the namespace ConsoleApplication1.TestXM LFile.xml and CSharpUtilityBelt.TestXMLF ile.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.
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.GetExecutingAssem
Note that the name needs to include the default namespace for the[/color][/color]
project.[color=blue][color
e.g. If the default namespace was My.Namespace and the file with its[/color][/color]
build[color=blue][color=gr
action set to "Embedded Resource" was called TextFile1.txt then you'd[/color][/color]
need[color=blue][color=gre
to pass "My.Namespace.TextFile1.tx
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]
relevant[color=green]
.mresource entry.
e.g.
.mresource public My.Namespace.TextFile1.txt
{
}"
Here is my code in ConsoleApplication1:
System.IO.Stream stream = System.Reflection.Assembly
I tried the namespace ConsoleApplication1.TestXM
ASKER
How do I get the DLL Assembly though, I've tried both namespaces with no luck.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
500 points is not enough....thank you very much.
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?