2014-04-17 127 views
0

为我的WIX安装程序编写自定义操作来读取包含我的配置数据的XML文件。这会更新一个系统配置文件。CustomAction DLL Wix读取XML VB 2012

我的问题是,当我运行安装程序时,它会在安装程序文件中查找我的XMl文件(temp.xml)。我希望这可以在安装程序正在运行的路径中找到它,以便我可以更改配置文件,而无需每次都重新构建MSI。

Public Shared Function CustomAction1(ByVal session As Session) As ActionResult 
     session.Log("Begin CustomAction1") 

     Dim installDir = Environment.GetEnvironmentVariable("EnactorInstall") 

     Dim doc As XmlDocument = New XmlDocument() 
     doc.Load("\Test.xml") 
     Dim root As XmlNode = doc.DocumentElement 
     Dim nodePorts As XmlNode = root.SelectSingleNode("/config/ports") 
     Dim BO As String = nodePorts.Attributes.ItemOf("BO").InnerText 
     Dim BP As String = nodePorts.Attributes.ItemOf("BP").InnerText 
     Dim EM As String = nodePorts.Attributes.ItemOf("EM").InnerText 
     Dim WS As String = nodePorts.Attributes.ItemOf("WS").InnerText 

     REM Modify enactor.Xml 
     Dim enactorXML = installDir & "config\ProcessingServer\enactor.xml" 
     Using file As New FileStream(enactorXML, FileMode.Open, FileAccess.ReadWrite) 
      REM read the file to memory 
      Dim reader As New StreamReader(file) 
      Dim content As String = reader.ReadToEnd() 

      REM replace tokens 
      content = Replace(content, "{ENVIRONMENT}", BO) 
      content = Replace(content, "{DEVICE_TYPE}", EM) 
      content = Replace(content, "{DEVICE_ID}", WS) 
      content = Replace(content, "{LOCATION_ID}", BP) 
      content = Replace(content, "{APPLICATION_HOME}", BO) 
      content = Replace(content, "{TRANSACTION_NUMBER}", EM) 
      content = Replace(content, "{SESSIONS}", EM) 
      content = Replace(content, "{RATE_BOARD_PORT}", BO) 

      REM clear the file 
      file.SetLength(0) 

      REM write back to the file 
      Dim writer As New StreamWriter(file) 
      writer.Write(content) 
      writer.Flush() 
      writer.Close() 
     End Using 

     Return ActionResult.Success 
    End Function 

回答

0

有你看着使用XMLConfig Element和由WixUtilExtension做你正在努力实现同样的事情提供的XMLFile Element?一探究竟。

+0

完全同意应该用于XML写入,但不支持XML读取。 –

0

,如果你的意思是你想上那就是在同一目录中MSI文件正在安装的文件运行它,然后拿到[SourceDir]属性到CA,这就是该文件是:

http://msdn.microsoft.com/en-us/library/aa371857(v=vs.85).aspx

但是如果您使用任何WiX捆绑软件,它可能不需要是自定义操作,因为您可能可能在文件安装之前运行它。

如果该文件属于Windows安装程序,因为它由MSI安装安装,请确保它没有文件哈希。文件哈希位于MSI文件中,如果更改了文件内容,然后MSI安装它,哈希将不匹配磁盘上的文件,并且会出现问题。这是msifiler是什么:

http://msdn.microsoft.com/en-us/library/aa370108(v=vs.85).aspx

+0

我希望它在我的文件被部署后运行。我现在一直试图让Wix设置环境变量来指定位置,然后让VB把它们提取出来并将它们追加到文件名之前。目前,如果我不运行客户操作,我可以获取应用的环境变量,但是当我在其中添加自定义操作时,不会写入它们。 – user2638540

+0

我给出的答案仍然适用,假设我明白你想要做什么。你不需要环境变量。在安装时将[SourceDir]写入注册表,这将解析为安装MSI的文件夹,您可以从应用程序的文件中复制该文件。或者使用MsiSourceListGetInfo()传递产品代码,并询问上次使用的源代码。据我所知,托管代码中没有提供MS。我仍然不清楚“在我的文件部署后”是否意味着在安装过程中或您的应用运行时的自定义操作。 – PhilDW

0

确定。今天我设法得到了这个工作。

我CustomActionData使用

Dim srcPath As String = session.CustomActionData("DataKey") 
    Dim srcPathInst As String = session.CustomActionData("DataKeyInst") 

我必须确保我的CA执行设置为推迟传递从我的WIX文件

<CustomAction Id="SetPathInst" Property="EnactorInstaller" Value="DataKey=[SourceDir];DataKeyInst=[INSTALLDIR]" /> 

,我可以再在我的阅读Vb的。我上面的例子允许我在一个自定义操作中传入多个属性值。然后,我还必须将此CA的属性设置为指向我的主CA的ID,并将其设置为首先执行。