17 sept 2010

Como hacer un servicio de Windows auto instalable

Todos nos hemos encontrado en la situación de tener que instalar un servicio, y utilizar el comando installutil.exe, pero por qué no ahorrarnos un paso y hacer que el servicio sea auto instalable?



Este tip no tiene mayor secreto, simplemente es un atajo al installutil.exe dentro del ejecutable de nuestro servicio.

Lo que haremos será ir a la clase Program.cs en C# o Módulo Main.vb eb VB y modificar el método main de la siguiente manera:


static class Program
{
///
/// The main entry point for the application.
///

static void Main(String[] args)
{
if (args.Length > 0)
{
switch (args[0])
{
case "/install":
install();
break;
case "/uninstall":
uninstall();
break;
}
}
else
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new SecurityProviderService() };
ServiceBase.Run(ServicesToRun);
}
}
}


Lo que hicimos fue agregarle a nuestro ejecutable la posibilidad de recibir parámetros, en éste caso /install o /uninstall, sólo nos resta crear los métodos para ejecutar éstas acciones y listo.


// Devuelve la ruta a installutil.exe
private static String installUtill()
{
return System.IO.Path.Combine(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(), "installutil.exe");
}

private static void uninstall()
{
if (System.IO.File.Exists(installUtill()))
{
System.Diagnostics.Process.Start(installUtill(), String.Format("/u \"{0}\"", System.Reflection.Assembly.GetExecutingAssembly().Location));
System.Diagnostics.Trace.WriteLine("Service uninstalled.");
}
else
throw new System.IO.FileNotFoundException("InstallUtil.exe not found");
}

private static void install()
{
if (System.IO.File.Exists(installUtill()))
{
System.Diagnostics.Process.Start(installUtill(), String.Format("\"{0}\"", System.Reflection.Assembly.GetExecutingAssembly().Location));
System.Diagnostics.Trace.WriteLine("Service installed.");
}
else
throw new System.IO.FileNotFoundException("InstallUtil.exe not found");
}


Muy simple, ahora cuando necesitemos instalar nuestro servicio simplemente lo haremos desde una consola ejecutando NombreDelServicio.exe /install

16 ago 2010

Referencia de atajos de teclado para Visual Studio 2010

Desde la web de Microsoft, nos ofrecen una serie de archivos PDF con los atajos de teclado para Visual Studio 2010.

C# shortcuts para Visual Studio 2010

Descargar