PC Review


Reply
Thread Tools Rate Thread

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

 
 
=?Utf-8?B?S2VubmV0aEJvaG1hbg==?=
Guest
Posts: n/a
 
      23rd Jun 2005
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

 
Reply With Quote
 
 
 
 
BertC
Guest
Posts: n/a
 
      23rd Jun 2005
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.

 
Reply With Quote
 
=?Utf-8?B?S2VubmV0aEJvaG1hbg==?=
Guest
Posts: n/a
 
      23rd Jun 2005
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.
--
Thanks,

Kenneth Bohman


"BertC" wrote:

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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the significance of link.exe.config and cl.exe.config files in VS2003 wn123456@gmail.com Microsoft Dot NET Framework 0 3rd May 2007 09:45 AM
Class library and config files...again OO Microsoft C# .NET 2 3rd Apr 2006 07:24 PM
XML config files for Class library No One Microsoft ASP .NET 2 6th Oct 2004 01:09 PM
NullReferenceException in call from c# class to c++ static class when touch web.config file AAguiar Microsoft ASP .NET 2 9th Jan 2004 01:26 PM
Class Library Reading from .config files Makarand Microsoft Dot NET Framework 1 29th Dec 2003 12:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:57 PM.