December 2, 2007

Deploying Unmanaged DLLs

Found this neat trick here Nice idea. Tried it and it works. Heres my version of the resource extractor:
public static class ResourceExtractor
{
    public static void ExtractResourceToFile(
      string resourceName, 
      string filename)
    {
        if (System.IO.File.Exists(filename))
        {
            System.IO.File.Delete(filename);
        }
        if (!System.IO.File.Exists(filename))
        {
            using (System.IO.Stream s = System.Reflection.Assembly.
               GetExecutingAssembly().GetManifestResourceStream(resourceName))
            {
                using (System.IO.FileStream fs = new System.IO.
                            FileStream(filename, System.IO.FileMode.Create))
                {
                    byte[] b = new byte[s.Length];
                    s.Read(b, 0, b.Length);
                    fs.Write(b, 0, b.Length);
                }
            }
        }
    }
}

No comments: