By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,139 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

How to get app.config info from code in a referenced class

P: n/a
I have a winforms project Vs2005 with one project - Project1 which is the
startup project and clsProject2 which is a class that is being used by
Project1 (an exception handler). In clsProject2 I have a procedure that must
take information from the app.config file of Project1. In the clsproject2 I
need to write code that gets the value of a setting in the app.config file
of Project1. I find that the code I had that seemed to work ijn Vs2003 now
no longer picks up the data in the Project1 app.config. Exactly how would I
be able to use code in clsProject2 to get configuration info from Project1
app.config.
I want to try to get the info via clsProject2 so that I can reuse the same
class without writing too much code in the calling projects to come later.

Any help would be really appreciated.

Thanks for any help.
Bob
Nov 15 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Robert Dufour" <bd*****@sgiims.comwrote in
news:e9**************@TK2MSFTNGP06.phx.gbl:
Exactly how would I
be able to use code in clsProject2 to get configuration info from
Project1 app.config.
I want to try to get the info via clsProject2 so that I can reuse the
same class without writing too much code in the calling projects to
come later.
clsProject2 is a library right?

In that case it'll run in the appDomain of it's parent process - thus it'll
use the parent's app.config.

So just go AppSetting("MyKey") - it'll pick it up from Project1's
app.config.
Nov 15 '06 #2

P: n/a
Thanks
Bob

"Spam Catcher" <sp**********@rogers.comwrote in message
news:Xn**********************************@127.0.0. 1...
"Robert Dufour" <bd*****@sgiims.comwrote in
news:e9**************@TK2MSFTNGP06.phx.gbl:
>Exactly how would I
be able to use code in clsProject2 to get configuration info from
Project1 app.config.
I want to try to get the info via clsProject2 so that I can reuse the
same class without writing too much code in the calling projects to
come later.

clsProject2 is a library right?

In that case it'll run in the appDomain of it's parent process - thus
it'll
use the parent's app.config.

So just go AppSetting("MyKey") - it'll pick it up from Project1's
app.config.

Nov 15 '06 #3

P: n/a
Hi, when I try that
Dim strTemp As String = AppSettings("mysetting")

I get the wavy blue underlines and "appsettings is a type and cannot be
used as an expression".

Bob

"Spam Catcher" <sp**********@rogers.comwrote in message
news:Xn**********************************@127.0.0. 1...
"Robert Dufour" <bd*****@sgiims.comwrote in
news:e9**************@TK2MSFTNGP06.phx.gbl:
>Exactly how would I
be able to use code in clsProject2 to get configuration info from
Project1 app.config.
I want to try to get the info via clsProject2 so that I can reuse the
same class without writing too much code in the calling projects to
come later.

clsProject2 is a library right?

In that case it'll run in the appDomain of it's parent process - thus
it'll
use the parent's app.config.

So just go AppSetting("MyKey") - it'll pick it up from Project1's
app.config.

Nov 15 '06 #4

P: n/a
"Robert Dufour" <bd*****@sgiims.comwrote in news:#Y9IKePCHHA.3836
@TK2MSFTNGP02.phx.gbl:
Hi, when I try that
Dim strTemp As String = AppSettings("mysetting")

I get the wavy blue underlines and "appsettings is a type and cannot be
used as an expression".
You need to import the ConfigurationSettings class (System.Configuration)
or the new Configuration Manager class (you need to reference the
System.Configuration DLL).
Nov 15 '06 #5

P: n/a
Thanks for your help. Still having a problem though. I referenced the new
configuration manager class. The wavy warning is gone and my code executes
without any exceptions, BUT it does not pick up any of the values in the
config file.
When I single step over the following line in the debugger strTemp remains
empty but there is a value for it in the config file.
Dim strTemp As String = AppSettings.GetString(strKey) - strkey is a
variable passed to this procedure, the spelling of the variable value is
correct.

Isn't high tech fun?

Thanks for your help

Bob

<Spam Catcher" <sp**********@rogers.comwrote in message
news:Xn**********************************@127.0.0. 1...
"Robert Dufour" <bd*****@sgiims.comwrote in news:#Y9IKePCHHA.3836
@TK2MSFTNGP02.phx.gbl:
>Hi, when I try that
Dim strTemp As String = AppSettings("mysetting")

I get the wavy blue underlines and "appsettings is a type and cannot be
used as an expression".

You need to import the ConfigurationSettings class (System.Configuration)
or the new Configuration Manager class (you need to reference the
System.Configuration DLL).

Nov 15 '06 #6

P: n/a
How 'bout the case of the variable. XML is case sensitive.

S

"Robert Dufour" <bd*****@sgiims.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Thanks for your help. Still having a problem though. I referenced the new
configuration manager class. The wavy warning is gone and my code executes
without any exceptions, BUT it does not pick up any of the values in the
config file.
When I single step over the following line in the debugger strTemp remains
empty but there is a value for it in the config file.
Dim strTemp As String = AppSettings.GetString(strKey) - strkey is a
variable passed to this procedure, the spelling of the variable value is
correct.

Isn't high tech fun?

Thanks for your help

Bob

<Spam Catcher" <sp**********@rogers.comwrote in message
news:Xn**********************************@127.0.0. 1...
>"Robert Dufour" <bd*****@sgiims.comwrote in news:#Y9IKePCHHA.3836
@TK2MSFTNGP02.phx.gbl:
>>Hi, when I try that
Dim strTemp As String = AppSettings("mysetting")

I get the wavy blue underlines and "appsettings is a type and cannot be
used as an expression".

You need to import the ConfigurationSettings class (System.Configuration)
or the new Configuration Manager class (you need to reference the
System.Configuration DLL).


Nov 15 '06 #7

P: n/a
Yes I know, I double checked, case of the variable is OK.
Bob
"Steve Long" <St**********@NoSpam.comwrote in message
news:uK**************@TK2MSFTNGP06.phx.gbl...
How 'bout the case of the variable. XML is case sensitive.

S

"Robert Dufour" <bd*****@sgiims.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>Thanks for your help. Still having a problem though. I referenced the new
configuration manager class. The wavy warning is gone and my code
executes without any exceptions, BUT it does not pick up any of the
values in the config file.
When I single step over the following line in the debugger strTemp
remains empty but there is a value for it in the config file.
Dim strTemp As String = AppSettings.GetString(strKey) - strkey is a
variable passed to this procedure, the spelling of the variable value is
correct.

Isn't high tech fun?

Thanks for your help

Bob

<Spam Catcher" <sp**********@rogers.comwrote in message
news:Xn**********************************@127.0.0 .1...
>>"Robert Dufour" <bd*****@sgiims.comwrote in news:#Y9IKePCHHA.3836
@TK2MSFTNGP02.phx.gbl:

Hi, when I try that
Dim strTemp As String = AppSettings("mysetting")

I get the wavy blue underlines and "appsettings is a type and cannot
be
used as an expression".

You need to import the ConfigurationSettings class
(System.Configuration)
or the new Configuration Manager class (you need to reference the
System.Configuration DLL).



Nov 16 '06 #8

P: n/a
Simple sample demonstrating the problem consists of a solution containing a
class project and a windows form project. I put the same in a new thread
also, hope thats OK.

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? By the way I tested in the
IDE and the exe itself. Both failed same way.

Thanks for any help

Bob

"Steve Long" <St**********@NoSpam.comwrote in message
news:uK**************@TK2MSFTNGP06.phx.gbl...
How 'bout the case of the variable. XML is case sensitive.

S

"Robert Dufour" <bd*****@sgiims.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>Thanks for your help. Still having a problem though. I referenced the new
configuration manager class. The wavy warning is gone and my code
executes without any exceptions, BUT it does not pick up any of the
values in the config file.
When I single step over the following line in the debugger strTemp
remains empty but there is a value for it in the config file.
Dim strTemp As String = AppSettings.GetString(strKey) - strkey is a
variable passed to this procedure, the spelling of the variable value is
correct.

Isn't high tech fun?

Thanks for your help

Bob

<Spam Catcher" <sp**********@rogers.comwrote in message
news:Xn**********************************@127.0.0 .1...
>>"Robert Dufour" <bd*****@sgiims.comwrote in news:#Y9IKePCHHA.3836
@TK2MSFTNGP02.phx.gbl:

Hi, when I try that
Dim strTemp As String = AppSettings("mysetting")

I get the wavy blue underlines and "appsettings is a type and cannot
be
used as an expression".

You need to import the ConfigurationSettings class
(System.Configuration)
or the new Configuration Manager class (you need to reference the
System.Configuration DLL).



Nov 16 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.