2016-01-27 25 views
0

我已經回答了this問題,因爲我需要在WIX安裝期間編輯文件,該文件不是xml文件。我正在通過wix部署一個網站,我需要根據用戶輸入對一個文件進行一些更改。在wix自定義操作中編輯inetpub文件夾中的文件

下面是我的自定義操作

<CustomAction Id="CustomActionID_Data" Property="CustActionId" Value="FileId=[#filBEFEF0F677712D0020C7ED04CB29C3BD];MYPROP=[MYPROP];"/> 

<CustomAction Id="CustActionId" 
     Execute="deferred" 
     Impersonate="no" 
     Return="ignore" 
     BinaryKey="CustomActions.dll" 
     DllEntry="EditFile" /> 

以下是自定義操作的代碼。

string prop= session.CustomActionData["MYPROP"];  
string path = session.CustomActionData["FileId"]; 
StreamReader f = File.OpenText(path); 
string data = f.ReadToEnd(); 
data = Regex.Replace(data, "replacethistext", prop); 
File.WriteAllText(path, data); // This throws exception. 

由於這是在IISs intetpub文件夾下,我的操作會拋出錯誤,表明該文件正在被另一個進程使用。任何解決方案

如果需要知道我的執行順序,它是在installfiles之後,所以站點尚未啓動但文件被複制。

<InstallExecuteSequence> 
    <Custom Action="CustomActionID_Data" Before="CustActionId">NOT REMOVE</Custom> 
    <Custom Action="CustActionId" After="InstallFiles">NOT REMOVE</Custom> 
</InstallExecuteSequence> 
+0

只是一個想法,WIX安裝程序本身已經掌握了該文件,自定義操作由於相同的原因無法修改它嗎?如果是的話那麼該怎麼辦? –

回答

0

好的,我解決了這個問題。這既不是問題問題,也不是執行順序。在上面的代碼中,我打開了一個流來閱讀文本,並且在閱讀後沒有處理它,這就是持有我的資源的東西。我將代碼更改爲下面,一切工作正常。

string data = ""; 
string prop= session.CustomActionData["MYPROP"];  
string path = session.CustomActionData["FileId"]; 
using(StreamReader f = File.OpenText(path)) // disposed StreamReader properly. 
{ 
    data = f.ReadToEnd(); 
    data = Regex.Replace(data, "replacethistext", prop); 
} 
File.WriteAllText(path, data); 
相關問題