Ian Blackburn

March 2005 Entries

Embedding javascript in an assembly

Recently I needed to embed some javascript into an assembly for distribution in an asp.net application. 

Here's how to embed the file:

  1. Create a js file such as WebUtils.js
  2. In Visual Studio, select the file in Solution Explorer and change the Build Action property to "Embedded Resource"
  3. Build the project and the WebUtils.js file is now part of the assembly (you don't need to distibute the js file now it's part of the assembly)

Now to get the resource out using code:

First thing is to determine the resource name.  This will depend on your namespace hierarchy.  If you are not sure use a tool like Reflector to find out, or you can use the code below to return a string array of all resource names in the current assembly:

public static string[] GetResourceNames()

{

Assembly a = Assembly.GetExecutingAssembly();

string [] resNames = a.GetManifestResourceNames();

Array.Sort(resNames);

return resNames;

}

So if we have a resource named Bbits.WebUtils.js we can use the following to include it in our page:

System.IO.Stream script = Assembly.GetExecutingAssembly().GetManifestResourceStream("Bbits.WebUtils.js");

System.IO.StreamReader sr = new System.IO.StreamReader(script);

Page.RegisterClientScriptBlock("WebUtils",sr.ReadToEnd());

sr.Close();

Have fun

Ian

Bitrates for online music stores

If like me you are very disappointed that the big players in online music stores such as msn and napster are only offering 128k bit rates (CD is upto 940) then you may want to try out this russian site:

www.allofmp3.com - they let you choose the bitrate (right up to cd quality) to download in and charge by the Mb.  Not only that it is very cheap, one cent per megabyte, so $10 will get you plenty of albums at a good quality.

So is it legal?  That appears to be a grey area...see http://www.theregister.co.uk/2004/05/05/russian_mp3_site/ for a discussion.

Cheers

Ian

.Net Survey and Poll Project

If you are looking for a good .net survey or poll tool, then take a look at NSurvey - it's free and open source and available here:

http://www.nsurvey.org/

I was stunned at the depth and detail in this product - and the implementation is just what you want.  A web based tool to create the survey which generates and xml file you use with a server contol in your site.  Neat.