There are many scenarios it is recommended to use an embedded resource which is part of the assembly. One of the common cases is having a custom control with custom script file and wanting to ship them in a close assembly to other projects. Thanks to deployments reasons we prefer to ship only the assembly without the other resource files. There is a simple approach to do it in .Net 2.0.
Ok, what do we have to do in order to accomplish this task?
Assumptions:
- We have a custom control which is called "AlertButton"(Inherit from the Button class).
- This control use javascript file which is called "alertUtils.js".
- The control is complied into "MyCustomControls.dll" assembly.
Tasks:
1. Define the javascript file as Embedded resource after the build action event:

2. Add a WebResource definition to the AssemblyInfo.
[assembly: System.Web.UI.WebResource([FileName], [ResourceType])]
For example:
[assembly: System.Web.UI.WebResource("MyCustomControls.alertUtils.js", "application/x-javascript")]
3. Then register this web resource in your web control:
[ToolboxData("<{0}:MyCustomControl runat=server></{0}:MyCustomControl>")]
public class MyCustomControl : Button
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
// For pages with ajax extension
ScriptManager.GetCurrent(Page).Scripts.Add(new ScriptReference
("MyCustomControls.alertUtils.js", " MyCustomControls"));
// For pages without ajax extension
Page.ClientScript.RegisterClientScriptInclude(
this.GetType(), "JavaScriptFileKey",
Page.ClientScript.GetWebResourceUrl(this.GetType(),
"MyCustomControls.alertUtils.js"));
}
}
4. And that is all!
On my last project I had a mission to find an appropriate framework for exporting some images and text to a Pdf file format.
The basic guidelines that lead me were:
- This framework should be developed under the .Net framework.
- It must be an open source code and not just black box.
- The export to a Pdf process shouldn't affect the exported image quality.
- The framework will be easily to use.
After searching over the net I found the solution => The ITextSharp framework answers all my requirements.
ITextSharp is a .Net open source libraries that allows you to generate a Pdf file in run time. I added a basic example for working with this framework:
public static void ExportToPdfFile(string )
{
// step 1: creation of a document-object
Document document = new Document();
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("MyPdfFile.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
Image jpeg = Image.getInstance(new Uri("http://itextsharp.sourceforge.net/examples/myKids.jpg"));
// step 4: we add the image to the document
document.Add(jpeg);
// step 5: we close the document
document.Close();
}
Defiantly, it's a powerful framework that allows you to export almost everything to a Pdf file.