Config File Project Output: Did they change this in 2005?

S

stand__sure

I had occasion tonight to write an installer class that changed
something in a config file tonight (I had never had a need to do it,
but happened upon a "How To" article that explained it in terms of a
web app and decided to try it with a regular app). What I discovered
was that for some reason the .config file is not being copied with
Primary Output. In checking a few other apps for which I had written
installers I found that they too lacked .config files. I could have
sworn that 2003 deployed these automatically -- am I wrong?

Here is the custom installer class that I used (it's just a tweek of
the m$ code)

Imports System.ComponentModel
Imports System.Configuration.Install

Public Class WebServiceInstaller

Public Overrides Sub Install( _
ByVal stateSaver As System.Collections.IDictionary)
MessageBox.Show("Started")
Dim installlog As New System.IO.StreamWriter("c:\Installation.log")
installlog.AutoFlush = True
Try
Dim ProvidedName As String =
Me.Context.Parameters.Item("ServerName")
Dim ServiceName As String =
Me.Context.Parameters.Item("ServiceName")
installlog.WriteLine("Starting edit of config file")
If (String.IsNullOrEmpty(ProvidedName) _
OrElse String.IsNullOrEmpty(ServiceName)) Then
Throw New InstallException("No arguments specified")
End If

'use reflection to find config file
Dim assem As System.Reflection.Assembly = _
System.Reflection.Assembly.GetExecutingAssembly()
Dim strConfigLoc As String = assem.Location
installlog.WriteLine(strConfigLoc)

Dim strTemp As String = strConfigLoc
strTemp = strTemp & ".config"

Dim finfo As New System.IO.FileInfo(strTemp)
installlog.WriteLine("File Info: " & strTemp)
If (Not finfo.Exists) Then
Throw New InstallException("Missing config file")
installlog.WriteLine("Missing config file")
Exit Sub
End If

'load document into xml dom
Dim xmldoc As New System.Xml.XmlDocument()
xmldoc.Load(finfo.FullName)

'find the right node and change the value
Dim nod As System.Xml.XmlNode
Dim FoundIt As Boolean = False
For Each nod In xmldoc.Item("configuration").Item("appSettings")
'skip comments
If nod.Name = "add" Then
If (nod.Attributes.GetNamedItem("key").Value = _
"CryptoWebServiceConsumer.CryptoWS.Service") Then
nod.Attributes.GetNamedItem("value").Value = _
"http://" & ProvidedName & "/" & ServiceName & "/Service.asmx"
FoundIt = True
End If
End If
Next nod

If (Not FoundIt) Then
Throw New InstallException("Config file did not contain a
ServerName section")
End If

xmldoc.Save(finfo.FullName)
Finally
installlog.WriteLine("Ending edit of config file")
installlog.Close()
End Try
End Sub
End Class
 
C

Chris Dunaway

If the file was app.config, then it is copied and renamed to
appname.exe.config. If it is something else it should be copied if you
have set its build action property to Content.
 
J

Jay B. Harlow [MVP - Outlook]

stand__sure,
Check the properties of you app.config file in solution explorer.

There is a new option that indicates Copy or Copy if Newer. I noticed it on
Sql Server 2005 Express data files.

It sounds like your app.config may be set to Copy if Newer, instead of
always copying...

Hope this helps
Jay


|I had occasion tonight to write an installer class that changed
| something in a config file tonight (I had never had a need to do it,
| but happened upon a "How To" article that explained it in terms of a
| web app and decided to try it with a regular app). What I discovered
| was that for some reason the .config file is not being copied with
| Primary Output. In checking a few other apps for which I had written
| installers I found that they too lacked .config files. I could have
| sworn that 2003 deployed these automatically -- am I wrong?
|
| Here is the custom installer class that I used (it's just a tweek of
| the m$ code)
|
| Imports System.ComponentModel
| Imports System.Configuration.Install
|
| Public Class WebServiceInstaller
|
| Public Overrides Sub Install( _
| ByVal stateSaver As System.Collections.IDictionary)
| MessageBox.Show("Started")
| Dim installlog As New System.IO.StreamWriter("c:\Installation.log")
| installlog.AutoFlush = True
| Try
| Dim ProvidedName As String =
| Me.Context.Parameters.Item("ServerName")
| Dim ServiceName As String =
| Me.Context.Parameters.Item("ServiceName")
| installlog.WriteLine("Starting edit of config file")
| If (String.IsNullOrEmpty(ProvidedName) _
| OrElse String.IsNullOrEmpty(ServiceName)) Then
| Throw New InstallException("No arguments specified")
| End If
|
| 'use reflection to find config file
| Dim assem As System.Reflection.Assembly = _
| System.Reflection.Assembly.GetExecutingAssembly()
| Dim strConfigLoc As String = assem.Location
| installlog.WriteLine(strConfigLoc)
|
| Dim strTemp As String = strConfigLoc
| strTemp = strTemp & ".config"
|
| Dim finfo As New System.IO.FileInfo(strTemp)
| installlog.WriteLine("File Info: " & strTemp)
| If (Not finfo.Exists) Then
| Throw New InstallException("Missing config file")
| installlog.WriteLine("Missing config file")
| Exit Sub
| End If
|
| 'load document into xml dom
| Dim xmldoc As New System.Xml.XmlDocument()
| xmldoc.Load(finfo.FullName)
|
| 'find the right node and change the value
| Dim nod As System.Xml.XmlNode
| Dim FoundIt As Boolean = False
| For Each nod In xmldoc.Item("configuration").Item("appSettings")
| 'skip comments
| If nod.Name = "add" Then
| If (nod.Attributes.GetNamedItem("key").Value = _
| "CryptoWebServiceConsumer.CryptoWS.Service") Then
| nod.Attributes.GetNamedItem("value").Value = _
| "http://" & ProvidedName & "/" & ServiceName & "/Service.asmx"
| FoundIt = True
| End If
| End If
| Next nod
|
| If (Not FoundIt) Then
| Throw New InstallException("Config file did not contain a
| ServerName section")
| End If
|
| xmldoc.Save(finfo.FullName)
| Finally
| installlog.WriteLine("Ending edit of config file")
| installlog.Close()
| End Try
| End Sub
| End Class
|
 
S

stand__sure

that part works as expected -- it's that myapp.exe.config is not being
picked up by the setup/deployment project
 
S

stand__sure

Jay,

I'm not seeing this option in the property pages... (I guess I should
get around to installing beta 2 which will probably eliminate this
issue)

Thanks,
stand__sure
 
J

Jay B. Harlow [MVP - Outlook]

stand__sure,
Doh! misread the part about "Primary output".

When I get back to my 2005 VPC I will try beta 2 to see how it behaves...

BTW: Have you tried asking in the VS 2005 communities?

http://forums.microsoft.com/msdn/

Hope this helps
Jay

| Jay,
|
| I'm not seeing this option in the property pages... (I guess I should
| get around to installing beta 2 which will probably eliminate this
| issue)
|
| Thanks,
| stand__sure
|
 

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