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
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