cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
gigolojoe
Level 4

How to edit web.config based on user input

Hi,
How do I edit a web.config based on user input?
I have a dialog that gets some information from the user.
Once the web.config is put down on the disk I want to change some values based on what the user selected at install time.
I tried using StrReplace but it doesn't seem to work.
I'm basically trying to do a simple find and replace in the web.config.
Any help will be much appreciated.
Thanks
Labels (1)
0 Kudos
(4) Replies
VinayMS
Level 3

You can do that using installscript. try open the file using installscript OpenFile API

Below sample code demo the opening the file and modifying the string.

EXAMPLE_FILE = "web.config";
EXAMPLE_DIR = INSTALLDIR;

if (OpenFile (nvFileHandle, EXAMPLE_DIR, EXAMPLE_FILE) < 0) then
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);

Give some proper string template for the user input value in web.config and use StrReplace API.

--
Vinay
0 Kudos
Superfreak3
Level 11

Our project is a Basic MSI and we do somewhat the same thing in that we edit our web.config at install runtime. We first install a seed web.config file containing placeholders such as !ComputerName! for example. Then we use the text edit function to replace the tags/placeholders with the Property value.

I guess the XML edit function could be used, but that requires certain versions of MSXML on the target system and we don't want to deal with another prerequisite at this time. Our update mechanism currently looks for an .msi so we can't simply bootstrap the requirement in a Setup.exe.

Eventually we may move to use a Setup.exe but that, of course is a different discussion.
0 Kudos
gigolojoe
Level 4

Thanks for the response.
I'm not sure what you mean by template in this line.
StrReplace (svContent, "{{SQLSERVERSETTING}}", szServer, 0);
What is the significance of the curly braces?
Thanks
0 Kudos
VinayMS
Level 3

It is just a template where you can pass the sql server name which you selected in the screen.
0 Kudos