2014-04-17 44 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,並將其設置爲首先執行。