Code Listings
Assemblies
Assembly manifest to request administrative elevation on Windows Vista:
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" />
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
Deploying assemblies outside the base folder:
using System;
using System.IO;
using System.Reflection;
public class Loader
{
static void Main()
{
AppDomain.CurrentDomain.AssemblyResolve += FindAssem;
// We must switch to another class before attempting to use
// any of the types in c:\ExtraAssemblies:
Program.Go();
}
static Assembly FindAssem (object sender, ResolveEventArgs args)
{
string simpleName = new AssemblyName (args.Name).Name;
string path = @"c:\ExtraAssemblies\" + simpleName + ".dll";
if (!File.Exists (path)) return null; // Sanity check
return Assembly.LoadFrom (path); // Load it up!
}
}
public class Program
{
public static void Go()
{
// Now we can reference types defined in c:\ExtraAssemblies
}
}
Packing a single-file executable:
using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
public class Loader
{
static Dictionary <string, Assembly> libs
= new Dictionary <string, Assembly>();
static void Main()
{
AppDomain.CurrentDomain.AssemblyResolve += FindAssem;
Program.Go();
}
static Assembly FindAssem (object sender, ResolveEventArgs args)
{
string shortName = new AssemblyName (args.Name).Name;
if (libs.ContainsKey (shortName)) return libs [shortName];
using (Stream s = Assembly.GetExecutingAssembly().
GetManifestResourceStream ("Libs." + shortName + ".dll"))
{
byte[] data = new BinaryReader (s).ReadBytes ((int) s.Length);
Assembly a = Assembly.Load (data);
libs [shortName] = a;
return a;
}
}
}
public class Program
{
public static void Go()
{
// Run main program...
}
}