ServicesResourcesConferences Our TeamWeblogsAboutContact
     
  

Developer Resources

  Architecture Briefings
  .NET Remoting FAQ
  Articles
  Conversations
  Tools and Samples
    WSCF
    DynWsLib
    HttpSysCfg
    .NET Remoting Projects
  Books

GACUtils

GACUtils is a small tool which comes in handy when developing ServicedComponents. It registers itself as a right-click command for *.DLL files and allows you to deploy/undeploy components from the GAC and to register/unregister them from COM+ catalog. It also gives you some more flexibility when importing components into the GAC, as it can modify VS.NET registry settings to include the deployed DLL in the "Add Reference/.NET Components" dialog.

Source code:

imports Microsoft.Win32

Module AssemblyReg

    Sub Main(args() as string)
        Dim shellKey as RegistryKey = Registry.ClassesRoot.CreateSubKey("dllfile\Shell\GacUtils\Command")
        shellKey.SetValue("", string.Format("{0} ""%1""", Reflection.Assembly.GetExecutingAssembly.Location))

        shellKey = Registry.ClassesRoot.CreateSubKey("dllfile\Shell\ILdasm\Command")
        shellKey.SetValue("", string.Format("{0} ""%1""", "ildasm.exe"))
        shellKey = Registry.ClassesRoot.CreateSubKey("exefile\Shell\ILdasm\Command")
        shellKey.SetValue("", string.Format("{0} ""%1""", "ildasm.exe"))

        if args.Length <> 1 then
            msgbox("Usage: GacUtils ", MsgBoxStyle.Information)
            exit sub
        End If

        dim ass as Reflection.Assembly = Reflection.Assembly.LoadFrom(args(0))
        dim assName as String = ass.FullName.Split(",")(0)

        dim isServicedComponent as Boolean
        isServicedComponent = ass.GetCustomAttributes(gettype(System.EnterpriseServices.ApplicationActivationAttribute), false).Length > 0

        dim menu as String = "1=Register in GAC,2=Unreg from GAC,3=Affect 'Add references' dialog"
        if isServicedComponent then menu &= ",4=Affect COM+ catalog"

        Console.WriteLine(menu.Replace(",", vbcrlf))
        Console.Write("  Multiple commands allowed: ")
        dim cmds as String = console.ReadLine
        dim p as Process

        if cmds.IndexOf("2") >= 0 then
            if cmds.IndexOf("4") >= 0 and isServicedComponent then AffectComPlus(false, assName)
            AffectGAC(false, assName)
            if cmds.IndexOf("3") >= 0 then AffectAddRefListing(assName)
        End If

        if cmds.IndexOf("1") >= 0 then
            AffectGAC(True, assName)
            if cmds.IndexOf("4") >= 0 and isServicedComponent then AffectComPlus(True, assName)
            if cmds.IndexOf("3") >= 0 then AffectAddRefListing(assName, IO.Path.GetDirectoryName(args(0)))
        End If

        if cmds = "3" then AffectAddRefListing(assName, IO.Path.GetDirectoryName(args(0)))
        if cmds = "4"  and isServicedComponent then AffectComPlus(True, assName)

        console.WriteLine
        console.Write(" to quit... ")
        console.readline
    End Sub


    private sub AffectGAC(register as Boolean, assemblyName as String)
        dim fmt as String = iif(register, "/i {0}.dll", "/u {0}")

        dim psi as New ProcessStartInfo("gacutil.exe", string.Format(fmt, assemblyName))
        psi.UseShellExecute = false

        dim p as Process = Process.Start(psi)
        p.WaitForExit
    End Sub

    private sub AffectComPlus(register as Boolean, assemblyName as String)
        dim fmt as String = iif(register, "{0}.dll", "/u {0}.dll")

        dim psi as New ProcessStartInfo("regsvcs.exe", string.Format(fmt, assemblyName))
        psi.UseShellExecute = false

        dim p as Process = Process.Start(psi)
        p.WaitForExit
    End Sub

    private sub AffectAddRefListing(assemblyName as String, optional path as string = "")
        console.WriteLine
        if path <> "" then
            console.WriteLine("Registering directory {0} for inclusion in 'Add references'", path)
            dim refKey as RegistryKey = Registry.LocalMachine.CreateSubKey("Software\Microsoft\.NETFramework\AssemblyFolders\" & assemblyName)
            refKey.SetValue("", path)
        else
            console.WriteLine("Unregistering directory {0} from 'Add references'", path)
            Registry.LocalMachine.DeleteSubKey("Software\Microsoft\.NETFramework\AssemblyFolders\" & assemblyName)
        End If
    End Sub
End Module






 

© 2002-2006 by Thinktecture, Ingo Rammer and Christian Weyer. All rights reserved. | Contact | Impressum