473,378 Members | 1,280 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,378 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 1725
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.