473,246 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

How do I: Dynamically embed resource files (specifically, XML) (...)

Hi, All. I am creating a setup program. The way it works is that the
user creates their setup info, my program generates the C# code for a
setup executable, embeds the xml file containing the info for the
setup, and compiles the whole thing into one EXE. How do i embed
resources, and access them, into that assembly?

Dec 30 '06 #1
8 13608
Hi,

When you compile the assembly with the C# compiler use a /resource switch
for each resource that you want to embed:

C# Language Reference
/resource (Embed Resource File to Output) (C# Compiler Options)
http://msdn2.microsoft.com/en-us/lib...07(VS.80).aspx

You can use the resgen tool to create binary resources from an xml resource
file (.resx) that you can supply to the C# compiler:

..NET Framework Tools
Resource File Generator (Resgen.exe)
http://msdn2.microsoft.com/en-us/lib...z1(VS.80).aspx

--
Dave Sexton
http://davesexton.com/blog

"CodeLeon" <le*******@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
Hi, All. I am creating a setup program. The way it works is that the
user creates their setup info, my program generates the C# code for a
setup executable, embeds the xml file containing the info for the
setup, and compiles the whole thing into one EXE. How do i embed
resources, and access them, into that assembly?

Dec 31 '06 #2
Thank you for your reply. It is much appreciated. So, how would I do
this programatically, usang an ICodeCompiler or similar class? How
would I use a simple (ie, serialized) XML file?

Thanks again!

Dave Sexton wrote:
Hi,

When you compile the assembly with the C# compiler use a /resource switch
for each resource that you want to embed:

C# Language Reference
/resource (Embed Resource File to Output) (C# Compiler Options)
http://msdn2.microsoft.com/en-us/lib...07(VS.80).aspx

You can use the resgen tool to create binary resources from an xml resource
file (.resx) that you can supply to the C# compiler:

.NET Framework Tools
Resource File Generator (Resgen.exe)
http://msdn2.microsoft.com/en-us/lib...z1(VS.80).aspx

--
Dave Sexton
http://davesexton.com/blog

"CodeLeon" <le*******@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
Hi, All. I am creating a setup program. The way it works is that the
user creates their setup info, my program generates the C# code for a
setup executable, embeds the xml file containing the info for the
setup, and compiles the whole thing into one EXE. How do i embed
resources, and access them, into that assembly?
Dec 31 '06 #3
Hi,

The following link contains an example:

CompilerParameters.EmbeddedResources Property
http://msdn2.microsoft.com/en-us/lib...resources.aspx

To create the resource file you can write it to a temporary file on disk for
use with the CodeDOM:

..NET Framework Class Library
ResourceWriter Class
http://msdn2.microsoft.com/en-us/lib...er(VS.80).aspx

Here's an example that I've derived based on the links above (your
serialized xml can be a byte[] or string here):

string temp = Path.Combine(Path.GetTempPath(),
Path.GetTempFileName());

try
{
using (FileStream stream = new FileStream(temp))
// Unfortunately, the EmbeddedResources property is a
// StringCollection and accepts only file paths, not resource
// streams, so MemoryStream can't be used here
{
using (ResourceWriter writer = new ResourceWriter(stream))
{
// your xml can be byte[] or a string
writer.AddResource("name", yourXml);
writer.AddResource("name2", yourXml2);
}
}

CompilerParameters cp = new CompilerParameters();

// provider is an instance of CodeDomProvider
if (provider.Supports(GeneratorSupport.Resources))
{
cp.EmbeddedResources.Add(temp);
}
...
CompilerResults cr =
provider.CompileAssemblyFromFile(cp, sourceFile);
...
}
finally
{
File.Delete(temp);
}
I haven't tested the code myself so beware :)

--
Dave Sexton
http://davesexton.com/blog

"CodeLeon" <le*******@gmail.comwrote in message
news:11**********************@k21g2000cwa.googlegr oups.com...
Thank you for your reply. It is much appreciated. So, how would I do
this programatically, usang an ICodeCompiler or similar class? How
would I use a simple (ie, serialized) XML file?

Thanks again!

Dave Sexton wrote:
>Hi,

When you compile the assembly with the C# compiler use a /resource switch
for each resource that you want to embed:

C# Language Reference
/resource (Embed Resource File to Output) (C# Compiler Options)
http://msdn2.microsoft.com/en-us/lib...07(VS.80).aspx

You can use the resgen tool to create binary resources from an xml
resource
file (.resx) that you can supply to the C# compiler:

.NET Framework Tools
Resource File Generator (Resgen.exe)
http://msdn2.microsoft.com/en-us/lib...z1(VS.80).aspx

--
Dave Sexton
http://davesexton.com/blog

"CodeLeon" <le*******@gmail.comwrote in message
news:11**********************@s34g2000cwa.googleg roups.com...
Hi, All. I am creating a setup program. The way it works is that the
user creates their setup info, my program generates the C# code for a
setup executable, embeds the xml file containing the info for the
setup, and compiles the whole thing into one EXE. How do i embed
resources, and access them, into that assembly?

Dec 31 '06 #4
Thank you! I do have a question: can EmbeddedResources contain binary
data, like for self-extraction, or text data, like for an html page?
Your links above really answered my question :) I appreciate your
reply!

Dave Sexton wrote:
Hi,

The following link contains an example:

CompilerParameters.EmbeddedResources Property
http://msdn2.microsoft.com/en-us/lib...resources.aspx

To create the resource file you can write it to a temporary file on disk for
use with the CodeDOM:

.NET Framework Class Library
ResourceWriter Class
http://msdn2.microsoft.com/en-us/lib...er(VS.80).aspx

Here's an example that I've derived based on the links above (your
serialized xml can be a byte[] or string here):

string temp = Path.Combine(Path.GetTempPath(),
Path.GetTempFileName());

try
{
using (FileStream stream = new FileStream(temp))
// Unfortunately, the EmbeddedResources property is a
// StringCollection and accepts only file paths, not resource
// streams, so MemoryStream can't be used here
{
using (ResourceWriter writer = new ResourceWriter(stream))
{
// your xml can be byte[] or a string
writer.AddResource("name", yourXml);
writer.AddResource("name2", yourXml2);
}
}

CompilerParameters cp = new CompilerParameters();

// provider is an instance of CodeDomProvider
if (provider.Supports(GeneratorSupport.Resources))
{
cp.EmbeddedResources.Add(temp);
}
...
CompilerResults cr =
provider.CompileAssemblyFromFile(cp, sourceFile);
...
}
finally
{
File.Delete(temp);
}
I haven't tested the code myself so beware :)

--
Dave Sexton
http://davesexton.com/blog

"CodeLeon" <le*******@gmail.comwrote in message
news:11**********************@k21g2000cwa.googlegr oups.com...
Thank you for your reply. It is much appreciated. So, how would I do
this programatically, usang an ICodeCompiler or similar class? How
would I use a simple (ie, serialized) XML file?

Thanks again!

Dave Sexton wrote:
Hi,

When you compile the assembly with the C# compiler use a /resource switch
for each resource that you want to embed:

C# Language Reference
/resource (Embed Resource File to Output) (C# Compiler Options)
http://msdn2.microsoft.com/en-us/lib...07(VS.80).aspx

You can use the resgen tool to create binary resources from an xml
resource
file (.resx) that you can supply to the C# compiler:

.NET Framework Tools
Resource File Generator (Resgen.exe)
http://msdn2.microsoft.com/en-us/lib...z1(VS.80).aspx

--
Dave Sexton
http://davesexton.com/blog

"CodeLeon" <le*******@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
Hi, All. I am creating a setup program. The way it works is that the
user creates their setup info, my program generates the C# code for a
setup executable, embeds the xml file containing the info for the
setup, and compiles the whole thing into one EXE. How do i embed
resources, and access them, into that assembly?
Dec 31 '06 #5
Hi,

It can be any format that you want. Use the
System.Resources.ResourceManager to load the resource later. e.g., Use the
GetStream method for binary or GetString method for a string resource.

--
Dave Sexton
http://davesexton.com/blog

"CodeLeon" <le*******@gmail.comwrote in message
news:11**********************@42g2000cwt.googlegro ups.com...
Thank you! I do have a question: can EmbeddedResources contain binary
data, like for self-extraction, or text data, like for an html page?
Your links above really answered my question :) I appreciate your
reply!

Dave Sexton wrote:
>Hi,

The following link contains an example:

CompilerParameters.EmbeddedResources Property
http://msdn2.microsoft.com/en-us/lib...resources.aspx

To create the resource file you can write it to a temporary file on disk
for
use with the CodeDOM:

.NET Framework Class Library
ResourceWriter Class
http://msdn2.microsoft.com/en-us/lib...er(VS.80).aspx

Here's an example that I've derived based on the links above (your
serialized xml can be a byte[] or string here):

string temp = Path.Combine(Path.GetTempPath(),
Path.GetTempFileName());

try
{
using (FileStream stream = new FileStream(temp))
// Unfortunately, the EmbeddedResources property is a
// StringCollection and accepts only file paths, not resource
// streams, so MemoryStream can't be used here
{
using (ResourceWriter writer = new ResourceWriter(stream))
{
// your xml can be byte[] or a string
writer.AddResource("name", yourXml);
writer.AddResource("name2", yourXml2);
}
}

CompilerParameters cp = new CompilerParameters();

// provider is an instance of CodeDomProvider
if (provider.Supports(GeneratorSupport.Resources))
{
cp.EmbeddedResources.Add(temp);
}
...
CompilerResults cr =
provider.CompileAssemblyFromFile(cp, sourceFile);
...
}
finally
{
File.Delete(temp);
}
I haven't tested the code myself so beware :)

--
Dave Sexton
http://davesexton.com/blog

"CodeLeon" <le*******@gmail.comwrote in message
news:11**********************@k21g2000cwa.googleg roups.com...
Thank you for your reply. It is much appreciated. So, how would I do
this programatically, usang an ICodeCompiler or similar class? How
would I use a simple (ie, serialized) XML file?

Thanks again!

Dave Sexton wrote:
Hi,

When you compile the assembly with the C# compiler use a /resource
switch
for each resource that you want to embed:

C# Language Reference
/resource (Embed Resource File to Output) (C# Compiler Options)
http://msdn2.microsoft.com/en-us/lib...07(VS.80).aspx

You can use the resgen tool to create binary resources from an xml
resource
file (.resx) that you can supply to the C# compiler:

.NET Framework Tools
Resource File Generator (Resgen.exe)
http://msdn2.microsoft.com/en-us/lib...z1(VS.80).aspx

--
Dave Sexton
http://davesexton.com/blog

"CodeLeon" <le*******@gmail.comwrote in message
news:11**********************@s34g2000cwa.googleg roups.com...
Hi, All. I am creating a setup program. The way it works is that the
user creates their setup info, my program generates the C# code for
a
setup executable, embeds the xml file containing the info for the
setup, and compiles the whole thing into one EXE. How do i embed
resources, and access them, into that assembly?


Jan 1 '07 #6
Okay! Thanks! That answers my question completely :).

Jan 1 '07 #7
How are you compiling the generated source code? Are you calling csc or
using the CSharpCodeProvider class? In the former case, you should specify
the /resource: option [2], in the latter, specify embedded resources with
the EmbeddedResouces [1] property of the CompilerParameters instance you are
using when creating the code provider.

[1]
http://msdn2.microsoft.com/en-us/lib...resources.aspx
[2]
http://msdn.microsoft.com/library/en...mpiler_topic12

"CodeLeon" <le*******@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
Hi, All. I am creating a setup program. The way it works is that the
user creates their setup info, my program generates the C# code for a
setup executable, embeds the xml file containing the info for the
setup, and compiles the whole thing into one EXE. How do i embed
resources, and access them, into that assembly?

Jan 1 '07 #8
I believe I am going with the latter, so the library handling the
dynamic generation can easily be deployed to PCs without direct access
to the csc. Thank You!

Lebesgue wrote:
How are you compiling the generated source code? Are you calling csc or
using the CSharpCodeProvider class? In the former case, you should specify
the /resource: option [2], in the latter, specify embedded resources with
the EmbeddedResouces [1] property of the CompilerParameters instance you are
using when creating the code provider.

[1]
http://msdn2.microsoft.com/en-us/lib...resources.aspx
[2]
http://msdn.microsoft.com/library/en...mpiler_topic12

"CodeLeon" <le*******@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
Hi, All. I am creating a setup program. The way it works is that the
user creates their setup info, my program generates the C# code for a
setup executable, embeds the xml file containing the info for the
setup, and compiles the whole thing into one EXE. How do i embed
resources, and access them, into that assembly?
Jan 1 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eric Barr | last post by:
I've found myself using XSD files for my datasets. I'm loading them up from disk right now. I was hoping there was a way to embed the file in the assembly, so I could have access to it without...
2
by: Greg Merideth | last post by:
I have an XSL file that I am using to transform some XML data using this method below. The problem is that if I use an embedded .XSL file in the .NET assembly, I get an error indicating that the...
2
by: David | last post by:
Hello, I'm using an xml schema (xsd file) to validate input xml. This xsd is used by the C# dll and must be distributed with it. The xsd isn't modified. So I would like to embed it in the DLL so...
9
by: Yama | last post by:
Hi, How can I dynamically embed the content of a filename.js into a .aspx page before rendering it to the client? Thanks, ~yamazed
0
by: scott.mankowitz | last post by:
Hi! Does anyone know if there is an easy way to embed fonts into a vb2005 application? Ideally, I would like to add the .ttf files as resources and just set the font using the forms designer. I...
1
by: Jesper | last post by:
Is it possible to embed an xml file from the solution in a program at compile time. Alternatively I will paste it in the code on the form doc.LoadXml( "<root>...a billion zillion...
12
by: TS | last post by:
i have a need to possibly enable mutli language support. What benefit do i get by using a resource file instead of a custom xml solution? thanks!
0
by: VigneshS | last post by:
Hi, I am a newbie to Globalization and Localisation Concepts. I tried almost all the methods of the Globalization concepts. But i cannot be able to embed a text file within a Resource. ...
1
by: TisMe | last post by:
Hi All, I am trying to use resource files to globalize my website application. I had this working fine, working through the API for setting page titles programatically, with the resource files...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.