1

How to create a Set Up which gets and save information dynamically in a config file while installing.The saved information is used in the application. For Ex: Get database name or FTP details as user input while an application is installed.

Tool Used: Visual Studio 2010

Niranjan Thangaiya
  • 495
  • 2
  • 10
  • 22

3 Answers3

0

You can modify settings in your app.config file at run and have those changes save permanently. For example, if are connecting to a database, your provide the server name and database name when your application starts up. See this post Update app.config system.net setting at runtime

Community
  • 1
  • 1
StackTrace
  • 9,190
  • 36
  • 114
  • 202
0

We have successfully used xslt to transform our Web.Config files with appropriate settings from an xml file. You can have a look at the following thread

Handling web.config differences across multiple machines when using version control

it details this. Would think you can do something similar with windows.

Something like this, name this file App.Config.xslt and invoke it passing in your settings this will generate and App.Config with the correct substitutions.

C#

   class Program
    {
        static void Main(string[] args)
        {
            XPathDocument xPathDocument = new XPathDocument(@"..\..\YourSettings.xml");
            XslCompiledTransform xslCompiledTransform = new XslCompiledTransform();
            xslCompiledTransform.Load(@"..\..\App.Config.xslt");
            XmlTextWriter writer = new XmlTextWriter(@"..\..\App.Config", null);
            xslCompiledTransform.Transform(xPathDocument, null, writer);
        }
    }

App.Config.xslt

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
<xsl:template match="YourSettings">
    <configuration>
      <connectionStrings>
        <add name="YourApp" connectionString="User Id={YourAppId};Password=&quot;{YourAppPassword}&quot;;Data Source={YourAppName}" />
      </connectionStrings>
</configuration>
</xsl:template>
</xsl:stylesheet>

and your configuration settings residing in another file

YourSettings.xml

<?xml version="1.0" encoding="utf-8" ?>
<YourSettings>
  <YourAppId>localhost</YourAppId>
  <YourAppPassword>password1234</YourAppPassword>
  <YourAppName>username</YourAppName>
</YourSettings>
Community
  • 1
  • 1
Jonathan
  • 2,318
  • 7
  • 25
  • 44
0

I have used custom action dlls very successfully to do what you are talking about.

If you google this you'll find many hits, one such example of how these work is here (seems to be quite decent in terms of showing you the structure).

The code inside your dll is just c#, so it can do pretty much anything you want it to do, dynamically writing to config files etc. You may have limitations, however, based upon the privileges of the account running the setup - for example most "user" accounts would not be able to write to c:\program files..... - which is why a lot of setup programs require to be run as an administrator.

Using the VS Setup project settings you can pass information from the setup into the Dll (note the inbound parameter on the Install override), although the amount of data you can pass is limited. I have typically passed in the install folder (so my dll knows where the files are to modify) and an "environment" setting, also usernames passwords etc., so I know what database to hit (and using what account to hit it), what account to run such-and-such a service under, etc. These can be captured through the limited range of dialogs you can introduce into the setup project itself.

This approach also meant that during development we did not have to worry too much about values in config files, since we knew they would be set properly during install. And especially with usernames and passwords, it meant that the developer didn't need to know them (the operator performing the install was the only person who know them) which was nice from a security perspective.

Note finally that there is also a hook here for uninstalls, so potentially if you need to undo something, there's a place to do it.

PeteH
  • 2,394
  • 1
  • 20
  • 29