Specify the add-in path in .addin file in an installer

By Xiaodong Liang

Issue

I am migrating myadd-ins to the new ‘Registry-Free’ technology. It requires an .addin file which configures the add-in such as location. Since most installers offer a way to override the install location I would like to dynamically change the path in the .addin file.

Solution

With relative path, the root searching path is <Inventor Installation path>bin. But most users prefer to other locations. So it is very useful to edit the .addin with the target location. If you are using Windows Setup to create the installer, the best is to take use of the installer class. Since Inventor .addin file is quite simple, we could just finds the <assembly> node directly and change the path.

My colleague Philippe delivered an AU class last year on the best practices of Inventor Add-in. He provides a useful template to create installer class. The package is available at

http://au.autodesk.com/?nd=class&session_id=9028

The code below is a simple sample in which there are add-in project MyFreeAddin and its Setup project. The Setup adds the .addin as one file which is to be installed to target folder. In the installer class, the codes finds the .addin file and changes the node <assembly> with the target location. Then it copies the .addin to the folder according to OS type and user. e.g. this sample assumes the OS is Win7 and for all users. In addition, it will delete .addin from the folder in uninstall or rollback.

 

Imports System.Runtime.InteropServices    Imports System.IO    Imports System.Reflection    Imports System.Xml    Imports System.Configuration.Install    Imports System.Configuration              Public Class AddInInstall             Public Sub New()            MyBase.New()                 'This call is required by the Component Designer.            InitializeComponent()                 'Add initialization code after the call to             ‘ Initialize Component             End Sub             ' install        Public Overrides Sub Install( _                ByVal stateSaver As                   System.Collections.IDictionary)                 Try                ' get addin location                Dim Asm As System.Reflection.Assembly =  _                       System.Reflection.Assembly. _                              GetExecutingAssembly                    Dim oAsmFullName As String = _                      System.IO.Path.GetDirectoryName( _                       Asm.Location) _                               + ""  + Asm.GetName().Name + ".dll"                     Dim oAddInFileName As String =  _                    "Autodesk." +  _                        Asm.GetName().Name + ".Inventor.addin"                     Dim oAddInFileFullName As String = _                    System.IO.Path.GetDirectoryName _                                  (Asm.Location)  _                        + "" + oAddInFileName                          If File.Exists(oAddInFileFullName) = _                    False Then                         Throw New InstallException( _                        "Cannot find .addin file!")                Else                         Dim doc As XmlDocument = New XmlDocument()                    doc.Load(oAddInFileFullName)                         Dim mynode As XmlNode = _                        doc.GetElementsByTagName _                             ("Assembly").Item(0)                         If mynode Is Nothing Then                        Throw New InstallException( _                            "Fail to find assembly node!")                    Else                        mynode.InnerText = oAsmFullName                    End If                    doc.Save(oAddInFileFullName)                              ' copy the addin to target folder                     ' according to OS type and all uses/separate                      ‘users                         ' e.g Win7 + for all users.                    Dim oDesFileFullName As String =  _                        "c:ProgramDataAutodeskInventor                           Addins" + _                            oAddInFileName                         If File.Exists(oDesFileFullName) Then                        File.Delete(oDesFileFullName)                    End If                         File.Copy(oAddInFileFullName,  _                           oDesFileFullName)                     End If            Catch                Throw New InstallException( _                    "Error when modify addin file!")            End Try                  End Sub             ' uninstall        Public Overrides Sub Uninstall( _            ByVal savedState As System.Collections.IDictionary)            MyBase.Uninstall(savedState)                  Try                Dim Asm As System.Reflection.Assembly = _                 System.Reflection.Assembly.GetExecutingAssembly                Dim oAddInFileName As String =  _                    "Autodesk." + Asm.GetName().Name +                 &
#160;     ".Inventor.addin"                    ' e.g Win7 + for all users.                Dim oDesFileFullName As String =  _                    "c:ProgramDataAutodeskInventor Addins" + _                        oAddInFileName                          If File.Exists(oDesFileFullName) Then                    File.Delete(oDesFileFullName)                     End If                 Catch ex As Exception                Throw New InstallException( _                    "Error when delete addin file!")            End Try        End Sub             Public Overrides Sub Rollback( _            ByVal savedState As System.Collections.IDictionary)            MyBase.Rollback(savedState)                 Try                Dim Asm As System.Reflection.Assembly = _                System.Reflection.Assembly.GetExecutingAssembly                    Dim oAddInFileName As String = _                    "Autodesk." + Asm.GetName().Name +  _                        ".Inventor.addin"                     ' e.g Win7 + for all users.                Dim oDesFileFullName As String =  _                    "c:ProgramDataAutodeskInventor Addins"  _                        + oAddInFileName                     ' delete the addin file                If File.Exists(oDesFileFullName) Then                    File.Delete(oDesFileFullName)                     End If                 Catch ex As Exception                Throw New InstallException( _                    "Error when delete addin file!")            End Try        End Sub              End Class

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading