PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

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

 
 
CodeLeon
Guest
Posts: n/a
 
      30th Dec 2006
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?

 
Reply With Quote
 
 
 
 
Dave Sexton
Guest
Posts: n/a
 
      31st Dec 2006
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/library/c0tyye07(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/library/ccec7sz1(VS.80).aspx

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

"CodeLeon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?
>



 
Reply With Quote
 
CodeLeon
Guest
Posts: n/a
 
      31st Dec 2006
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/library/c0tyye07(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/library/ccec7sz1(VS.80).aspx
>
> --
> Dave Sexton
> http://davesexton.com/blog
>
> "CodeLeon" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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?
> >


 
Reply With Quote
 
Dave Sexton
Guest
Posts: n/a
 
      31st Dec 2006
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/library/system.resources.resourcewriter(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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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/library/c0tyye07(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/library/ccec7sz1(VS.80).aspx
>>
>> --
>> Dave Sexton
>> http://davesexton.com/blog
>>
>> "CodeLeon" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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?
>> >

>



 
Reply With Quote
 
CodeLeon
Guest
Posts: n/a
 
      31st Dec 2006
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/library/system.resources.resourcewriter(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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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/library/c0tyye07(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/library/ccec7sz1(VS.80).aspx
> >>
> >> --
> >> Dave Sexton
> >> http://davesexton.com/blog
> >>
> >> "CodeLeon" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed)...
> >> > 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?
> >> >

> >


 
Reply With Quote
 
Dave Sexton
Guest
Posts: n/a
 
      1st Jan 2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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/library/system.resources.resourcewriter(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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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/library/c0tyye07(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/library/ccec7sz1(VS.80).aspx
>> >>
>> >> --
>> >> Dave Sexton
>> >> http://davesexton.com/blog
>> >>
>> >> "CodeLeon" <(E-Mail Removed)> wrote in message
>> >> news:(E-Mail Removed)...
>> >> > 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?
>> >> >
>> >

>



 
Reply With Quote
 
CodeLeon
Guest
Posts: n/a
 
      1st Jan 2007
Okay! Thanks! That answers my question completely .

 
Reply With Quote
 
Lebesgue
Guest
Posts: n/a
 
      1st Jan 2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?
>



 
Reply With Quote
 
CodeLeon
Guest
Posts: n/a
 
      1st Jan 2007
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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?
> >


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to embed resource in ASP.Net 2.0 web application? Tomasz J Microsoft ASP .NET 8 13th Jun 2007 04:20 PM
Embed resource shapper Microsoft ASP .NET 1 14th Nov 2006 01:42 AM
How to use the wav file embed in resource? yxq Microsoft VB .NET 2 6th Nov 2004 04:31 AM
How to embed any resource in ready EXE.. Jigar Mehta Microsoft VC .NET 1 9th Feb 2004 08:05 PM
How do Embed a file as a resource? M Microsoft C# .NET 3 31st Dec 2003 07:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:51 PM.