How to use .config-files in the Install class?

G

Guest

Hi everybody,

Question: how can I use the executable.config-files during installation? The
answer might seem obvious, but there is apparently more to it!

My itention was, as part of the iinstallation, to write write two settings
to the .config file for the executable. I don't usually do that, but for such
a small application I thought it would be alright.

I created a simple Install class, added an override for the Install
procedure. They both executed during the installation, but an exception was
thrown when my own class for writing to .config-files was called. Not
suceeding to finding the reason for that, I switched into just reading the
..config-file, which has rather added to my confusion than to my happiness.
Whatever method I use no <appSettings> are returned.

Here is a base version of the testing code I'm using. Observe that the
..config-file is found and read correctly, but the appSettings are not. That
means I can neither read nor write to the file in the usual manner. I suppose
I can still deal with it as any text file.
I've stripped all three methods for reading the <appSettings>, that I've
tried and know well how to use:
Configuration.ConfigurationSettings + AppSettings
Configuration.ConfigurationSettings + GetConfig
Configuration.AppSettingsReader + GetValue
Please Feel free to add whatever method you find suitable for the job

Public Overrides Sub Install(ByVal stateSaver As
System.Collections.IDictionary)
MyBase.Install(stateSaver)

Try
Dim Asm As System.Reflection.Assembly =
System.Reflection.Assembly.GetExecutingAssembly
If Not System.IO.File.Exists(Asm.Location + ".configs") Then
Throw New InstallException(Asm.Location + ".configs" & " is
missing")
End If

Dim configSettings As
System.Collections.Specialized.NameValueCollection
configSettings =
System.Configuration.ConfigurationSettings.GetConfig("appSettings")
If configSettings Is Nothing Then
MsgBox("No appsettings")
Else
Dim i As Integer
Dim sb As New System.Text.StringBuilder()
For i = 0 To configSettings.Keys.Count - 1
sb.Append(configSettings.Item(i).ToString)
If i < configSettings.Keys.Count - 1 Then
sb.Append(",")
End If
Console.WriteLine(configSettings.Item(i).ToString)
Next
MsgBox(sb.ToString)
End If

ReadTextFile(Asm.Location + ".config")

Catch ex As Exception
Throw New Exception(ex.Source & vbCrLf & ex.Message & vbCrLf &
ex.TargetSite.ToString)
End Try
End Sub

Private Sub ReadTextFile(ByVal filename As String)
Dim Sr As IO.StreamReader = New
IO.StreamReader(IO.File.OpenRead(filename), System.Text.Encoding.Default)
Dim sb As New System.Text.StringBuilder()
While Sr.Peek <> -1
Try
sb.Append(Sr.ReadLine() & vbCrLf)
Catch ex As Exception
Throw New Exception(ex.Source & vbCrLf & ex.Message & vbCrLf
& ex.TargetSite.ToString)
End Try
End While
Sr.Close()
MsgBox(sb.ToString)
End Sub

MyProgram.exe.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="MyKey1" value="Value1" />
<add key="MyKey2" value="Value2" />
</appSettings>
</configuration>
--


Thanks for your help,

Kenneth Bohman
 
B

BertC

Hi Kenneth,

The GetConfig returns the entries for the config file of the current
executable, but when you are installing, the currently running
executable is not your program, but the installer. For example, if you
are using InstallUtil.exe, then you would be reading the appconfig
settings of that program, not your own.

As far as I know, the only way to update the .config files at install
time is to open them either using the XmlDocument.Load method or using
straight file reads.
 
G

Guest

I was doing that kind of thinking and I checked GetCallingAssembly and
GetEntryAssembly to get a better understanding. Then again, since
GetExecutingAssembly does return the name of my program, I still expected my
program.exe.config to be executed. Why would myprogram.exe and
installxxx.exe.config be paired? It didn't stack up to me. You see what I
mean?

What I've done now is to do the settings when the application is launched.
That way I can add some validation as well, which is never a bad thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top