2010-08-24 59 views
2

如何在installshield 2010中通過installscript修改app.config文件。 我想根據用戶選擇的服務器修改app.config文件中的sql連接字符串。 早期的幫助應該被讚賞。通過installscript修改app.config

回答

1

我已經編寫了自己的installscript來更改連接字符串。

代碼片段:

function Update_AppConfig(hMSI) 
STRING  EXAMPLE_FILE,  EXAMPLE_DIR; 
STRING svLine; 
STRING svContent; 
number nvBuff,temp,nvFileHandle ; 
string szConnection,szServer,szFileName,szUser,szFilePath; 
long cchValueBuf; 
long length;        
begin    

EXAMPLE_FILE = "Example.exe.config"; 
EXAMPLE_DIR = INSTALLDIR; 

cchValueBuf = 2;   

MsiGetProperty(hMSI,"IS_SQLSERVER_SERVER",szServer,cchValueBuf); 

OpenFileMode (FILE_MODE_NORMAL);  

if (OpenFile (nvFileHandle, EXAMPLE_DIR, EXAMPLE_FILE) < 0) then 
     MessageBox ("OpenFile failed.", SEVERE); 
     abort;         
    endif; 

    while GetLine (nvFileHandle, svLine) = 0 
     svContent = svContent + svLine + "\r\n"; 
    endwhile; 

    if (CloseFile (nvFileHandle) < 0) then   
     MessageBox ("CloseFile failed.", SEVERE);  
    endif; 

    StrReplace (svContent, "{{SQLSERVERSETTING}}", szServer, 0);  


// Close the file.       

OpenFileMode (FILE_MODE_BINARY);  

if (OpenFile (nvFileHandle, EXAMPLE_DIR, EXAMPLE_FILE) < 0) then  
     MessageBox ("OpenFile failed.", SEVERE);   
     abort;       
    endif;   

    length = StrLength (svContent); 

    if (WriteBytes (nvFileHandle, svContent, 0, length) < 0) then 
     MessageBox ("WriteBytes failed.", SEVERE);   
    endif; 

    if (CloseFile (nvFileHandle) < 0) then 
     MessageBox ("CloseFile failed.", SEVERE); 
    endif;  

end;