473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

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.GetConf ig("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

Jul 21 '05 #1
2 1733
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.

Jul 21 '05 #2
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.

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Suresh Gladstone | last post by:
Hi, This is a bit with versioning and installation of the .NET dlls. I want to perform the following, 1. A third party application will be invoking my .NET dll through COM interop . For this I...
5
by: George | last post by:
Hi, Is it possible to specify the location of a web.config file in an ASP.NET application? I have one ASP.NET application which is to be hosted on 2 web sites in the same web server. I want...
8
by: Robert | last post by:
I have what I think should be a pretty common scenario in a corporate environment. I have a web app and three environments on three servers: Development, QA and Production. We develop in...
6
by: Rich | last post by:
Hello, I picked up this example on using the Reflection namespace for loading forms/classes on the fly at msdn http://msdn.microsoft.com/library/default.asp?...
3
by: Richard Lewis Haggard | last post by:
I have a test application that is calling an assembly that reads some strings out of a config file. Normally, this assembly supports a web application and the information can be read just fine....
13
by: Khodr | last post by:
Hello, I am using VS.NET 2003 and vb. I build my application MyApp and it generates MyApp.exe.config. So now MyApp.exe reads parameters from MyApp.exe.config. Great and no problem! I need to...
3
by: Blasting Cap | last post by:
I am working on a web app that I want to be able to use a separate config file on, in addition to the web.config file that's already working in the application. If I put the following in the...
5
by: mmcd79 | last post by:
I built a VB.net application that makes use of a machine level DB connection string setting, and a user level starting location setting. The machine level setting and the default user based...
10
by: eagle | last post by:
I have a web.config in my application that contains the connection strings to all my datasources. I want to move these connection strings to another web config up the folder hierarchy so that all...
2
by: Johnson | last post by:
I'm trying to fix a "sub optimal" situation with respect to connection string management. Your thoughtful responses will be appreciated. I just started with a new client who has a bunch of legacy...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.