Re: appsetting in class doesn't work
- From: "rdufour" <bdufour@xxxxxxxxxx>
- Date: Fri, 17 Nov 2006 19:53:43 -0500
I'm going to control myself in commenting on MS <GGG>.
In 2003 I could easily change the config file settings from inside the app,
so I could have an application that on start would test its connection
string for instance and if it could not connect it would bring up a
configuration string modification and testing screen, so if the admin
changed the server name, no problem, easy to fix. In 2005 standard
application scope settings can't be changed. Nice going. MS just decided
that you should not do that. Now we gotta play in the config files with
notepad, Thanks a lot and it just gets better as you go along.
Anyways, yes Steve I would appreciate a copy of the assemblies you made, I
will surely try to use them.
Bob
"Steve Long" <Steve_Noneya@xxxxxxxxxx> wrote in message
news:OzPI5uoCHHA.4464@xxxxxxxxxxxxxxxxxxxxxxx
Robert, if you read this article:
http://visualbasic.about.com/od/usingvbnet/a/appsettings.htm
I think it may make you want to hurl at what MS has done with
configuration files in general.
I created an assembly for use in my applications to handle application
settings. It uses the tag format of VS2003 and allows for multiple
sections. The main difference in this assembly is that you pass in the
name of the section along with the name of the key to get the value for
said key.
Additionally, at a later time, I realized that I needed the capability to
create these config files on the fly, since that time, there was a second
class placed in the assembly CAppInit2 that will do just that.
Other than that, it works pretty much like the old config files worked.
But, you pass in the pathFileName to load a configuration file (a bit of
freedom right there), and, you can change a config file, while the app is
running (outside of the app), and this assembly will pickup those changes
and reload itself. Let me know if you want the code, I'd be happy to share
as this stuff that MS has done just looks horrific to me.
S
"Robert Dufour" <bdufour@xxxxxxxxxx> wrote in message
news:Oqy6OGnCHHA.1220@xxxxxxxxxxxxxxxxxxxxxxx
Ok guys thanks to you both I found a way for now, far from elegant but it
works at least in my test app.
This is the typical full app.config file in Vs2005. See the bottom I
added an appsettings section.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="TestClassApConfig.My.MySettings"
type="System.Configuration.ClientSettingsSection, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
</sectionGroup>
</configSections>
<system.diagnostics>
<sources>
<!-- This section defines the logging configuration for
My.Application.Log -->
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLog"/>
<!-- Uncomment the below section to write to the Application Event
Log -->
<!--<add name="EventLog"/>-->
</listeners>
</source>
</sources>
<switches>
<add name="DefaultSwitch" value="Information" />
</switches>
<sharedListeners>
<add name="FileLog"
type="Microsoft.VisualBasic.Logging.FileLogTraceListener,
Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
initializeData="FileLogWriter"/>
<!-- Uncomment the below section and replace APPLICATION_NAME with the
name of your application to write to the Application Event Log -->
<!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener"
initializeData="APPLICATION_NAME"/> -->
</sharedListeners>
</system.diagnostics>
<applicationSettings>
<TestClassApConfig.My.MySettings>
<setting name="TestSetting" serializeAs="String">
<value>Testvalue1</value>
</setting>
</TestClassApConfig.My.MySettings>
</applicationSettings>
<appSettings>
<add key="TestSetting" value="TestValue"/>
</appSettings>
</configuration>
In the class project, I use the code that Steve gave me. Class code
snippet is below
Imports System.Configuration.ConfigurationManager
Imports System.Configuration
Public Class Class1
Public _strTestSetting As String
Public Sub SetTestsetting()
Dim s As String
Dim cnfg As System.Configuration.Configuration
Dim el As KeyValueConfigurationElement
cnfg =
System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
el = cnfg.AppSettings.Settings.Item("TestSetting")
s = el.Value.ToString()
_strTestSetting = s
End Sub
End Class
This code used in the second class project picks up the values in the
app.config file of the startup project in the section appsettings. In the
startup project I can use My.settings("TestSetting") to pick up the value
in the applicationSettings section. Its a bit of a kludge and it means I
will have to make sure the values in the two sections are synchronized,
but its workable until I find a way to get rid of the appSettings section
in the app.config file and use only the section.
<TestClassApConfig.My.MySettings>
<setting name="TestSetting" serializeAs="String">
<value>Testvalue1</value>
</setting>
</TestClassApConfig.My.MySettings>
Which is the new XML format in Vs2005
Bob
"Steve Long" <Steve_Noneya@xxxxxxxxxx> wrote in message
news:%23AayAadCHHA.3924@xxxxxxxxxxxxxxxxxxxxxxx
Hi Robert. Please forgive me if I'm making assumptions here but it sort
of sounds like there's a difference here between the app.config file and
the settings enter in the designer. It looks to me, (remember I have
little experience with this), that this whole thing can get easily
mucked up. So, I started a new project, click on the project in the in
the solution expolorer, clicked properties, clicked settings, enter the
"TestSetting" setting and gave it a default value of "TestValue", then
in the Form Load event, I just did this:
Dim s As String = My.Setting.TestSetting
and it returned the TestValue value.
That was it.
Steve
"Robert Dufour" <bdufour@xxxxxxxxxx> wrote in message
news:uNKLrDdCHHA.3448@xxxxxxxxxxxxxxxxxxxxxxx
I tried via My.settings can't seem to get to it. :-(
Jeez has anyone at MS ever heard of KISS? Like in Keep It Simple
Stupid.
I'm starting to think the only way to get this project off the ground
and working is to go back to Visual Studio 2003 or even Vs 6.
Hope someone can come up with a way to get to these values in the new
XML format being used by the app.config files in Vs2005.
Thanks
Bob
"Steve Long" <Steve_Noneya@xxxxxxxxxx> wrote in message
news:OpqQS6bCHHA.4928@xxxxxxxxxxxxxxxxxxxxxxx
Well isn't that nice of MS to take the confusion to, yet, another
level... :)
You know, you might be able to get at those setting via the
My.Settings functionality.
Alternatively here's a microcosm of my app.config file, which I
included by right cliking on my project and clicking add/ New Item and
selecting the Application Configuration item.
<configuration>
<appSettings>
<add key="TestSetting" value="TestValue"/>
</appSettings>
</configuration>
Check it out
S
"Robert Dufour" <bdufour@xxxxxxxxxx> wrote in message
news:ODxkS1bCHHA.1012@xxxxxxxxxxxxxxxxxxxxxxx
Hi steve,
When running it I'm getting an unhandled exception error on line
s = el.Value.ToString() Object reference not set to an instance of an
object. Thats because with the line
el = cnfg.AppSettings.Settings.Item("TestSetting")
el stays at nothing and thats no doubt because my app.config file is
using the new format generated by the settings designer in VS2005
which is as follows.
<applicationSettings>
<TestClassApConfig.My.MySettings>
<setting name="TestSetting" serializeAs="String">
<value>Testvalue</value>
</setting>
</TestClassApConfig.My.MySettings>
</applicationSettings>
And indeed when I tested it using the old style app.config file
<appSettings>
<add key="TestSetting" value="TestValue"/>
</appSettings>
the code works fine.
The question now becomes, how do you retrieve the TestSetting value
when you created your app config file using the built in tools of
VS2005 - creating a new app.config file with add item-new item-
application configuration and putting application scope settings in
the file by using the Myproject - settings to write settings while
developping in the IDE.
Thanks for your help,
Bob
"Steve Long" <Steve_Noneya@xxxxxxxxxx> wrote in message
news:esZh6WbCHHA.3524@xxxxxxxxxxxxxxxxxxxxxxx
Robert, this is my first foray into the configuration system of .NET
2.0 and it seem ridiculously difficult to me but here's how I got
the value of the item that you are trying to retrieve.
First the config file:
<appSettings>
<add key="TestSetting" value="TestValue"/>
</appSettings>
Now the code:
Dim s As String
Dim cnfg As System.Configuration.Configuration
Dim el As KeyValueConfigurationElement
cnfg =
System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
el = cnfg.AppSettings.Settings.Item("TestSetting")
s = el.Value.ToString()
This does indeed return the value that I have "TestValue" in the
config file.
HTH
S
P.S. It seems much easier in VS2003
"Robert Dufour" <bdufour@xxxxxxxxxx> wrote in message
news:eGfK2daCHHA.1012@xxxxxxxxxxxxxxxxxxxxxxx
Here's the class code snippet
Imports System.Configuration.ConfigurationManager
Public Class Class1
Public _strTestSetting As String
Public Sub SetTestsetting()
_strTestSetting = AppSettings.Get("TestSetting") -should get the
value of TestSetting in app config file
End Sub
End Class
This is the snippet in the project calling the class -Project
TestIt - To test you should have a setting named TestSetting
application scope, value Testvalue
Imports System.Configuration
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim mycls As New clsTestconfig.Class1
mycls.SetTestsetting()
Label1.Text = mycls._strTestSetting
End Sub
End Class
There's a setting TestSetting in the settings file of the project
Testit
Put a breakpoint on line
_strTestSetting = AppSettings.Get("TestSetting")
Click the button, you will see as you step over the breakpoint that
the value of _strTestSetting stays at nothing. ie, its not picking
up the value of TestSetting in app.config.
How can I get this pickup of the value to work?
Thanks for any help
Bob
.
- References:
- appsetting in class doesn't work
- From: Robert Dufour
- Re: appsetting in class doesn't work
- From: Steve Long
- Re: appsetting in class doesn't work
- From: Robert Dufour
- Re: appsetting in class doesn't work
- From: Steve Long
- Re: appsetting in class doesn't work
- From: Robert Dufour
- Re: appsetting in class doesn't work
- From: Steve Long
- Re: appsetting in class doesn't work
- From: Robert Dufour
- Re: appsetting in class doesn't work
- From: Steve Long
- appsetting in class doesn't work
- Prev by Date: Re: appsetting in class doesn't work
- Next by Date: How to attach an exe to the debugger?
- Previous by thread: Re: appsetting in class doesn't work
- Next by thread: passing parameter to class from application
- Index(es):