Search code examples
installshield-2010

Modifying app.config through installscript


How to modify app.config files through installscript in installshield 2010. I want to modify the sql connection string in app.config file based on user selected server. Early help should be appreciated.


Solution

  • I have written own installscript to change the connection string.

    Code snippet :

    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;