Its good practice to separate out code that can be re-used into its own class library which can easy be referenced by other projects. Until now any resources such as images/JavaScript files that I'd created I needed to remember to copy to each web site that used the dll, ensuring that I created the correct directory structure so the paths would be correct. Then it dawned on me that you must be able to embed these files into the dll itself. Here's the steps I used to embed an image file for a AjaxControlTookit Calendar Extender that I was using in a custom server control (the same principle can be applied to any file you need - typically JavasScript and/or images will be embedded)
Step 1:
Add your file to the Class Library project
(in my example I added [PROJECT ROOT]/Resources/Calendar.png")
Step 2:
Select the newly added file and in the Properties window select:
Build Action = "Embedded Resource";
Step 3:
In the Project's AssemblyInfo.cs file add the following line:
//Replace "BradsNamespace" with the namespace of your project
//Replace "Resources" with the folder structure from the root of the project
//to the file (using . instead of / )
//Replace "Calendar.png" with your file's name
//Replace "img/png" with relevant type e.g. "text/javascript", "image/jpeg" etc
[assembly: System.Web.UI.WebResource(
"BradsNamespace.Resources.Calendar.png", "img/png")]
Step 3:
Now retrieve the Url for the file:
Image CalendarImage = new Image();
CalendarImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(
this.GetType(),
//Replace following with the string entered in AssemblyInfo.cs
"BradsNamespace.Resources.Calendar.png");
Finished:
And that's it! Now when you run it .NET will generate the Url for you in the format:
src="/WebResource.axd?d=_Y_kbnu3Fp4-fBKDGxpOa-LgnKcgPOIASf1ExUjshsj3373650125705280"