471,315 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Using Settings across an application C# 2.0

I currently use statements like this in my 1.1 code.

System.Configuration.ConfigurationSettings.AppSett ings[ "jobclock.net" ]

I use this in DLLs that are referenced by the application. The application
has this data in its app.config file. I would like to switch to the new
Setting files in 2.0, but I don't know if I can access these settings from a
different DLL. I can't make a reference in the DLL back to the application
because that would create a circular reference.

Anyone done this or have any ideas?

Thanks,

Eric Renken


Dec 27 '05 #1
13 1771
Eric,

What do you have to set a reference back to the application for? The
line that you referenced in your post is from the ConfigurationSettings
class in the System.Configuration namespace, and the dll that is in
certainly doesn't have a reference to your dll, so there should be no
problem.

I don't see why you would have to change this. If your DLL is loaded,
you can access the settings in the file the same as any other assembly
running in the app domain.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Eric Renken" <Er*********@newsgroup.nospam> wrote in message
news:uc**************@tk2msftngp13.phx.gbl...
I currently use statements like this in my 1.1 code.

System.Configuration.ConfigurationSettings.AppSett ings[ "jobclock.net" ]

I use this in DLLs that are referenced by the application. The
application has this data in its app.config file. I would like to switch
to the new Setting files in 2.0, but I don't know if I can access these
settings from a different DLL. I can't make a reference in the DLL back
to the application because that would create a circular reference.

Anyone done this or have any ideas?

Thanks,

Eric Renken

Dec 27 '05 #2
Right I don't have to set a reference back for using
ConfirationSettings.AppSettings[], but the problem is that method is
obsolete in 2.0 and I am trying to use 2.0 settings file now and I want to
know how to use the settings from my EXE in a DLL that is referenced by the
EXE without passing that data to it.

Eric Renken

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:el**************@TK2MSFTNGP11.phx.gbl...
Eric,

What do you have to set a reference back to the application for? The
line that you referenced in your post is from the ConfigurationSettings
class in the System.Configuration namespace, and the dll that is in
certainly doesn't have a reference to your dll, so there should be no
problem.

I don't see why you would have to change this. If your DLL is loaded,
you can access the settings in the file the same as any other assembly
running in the app domain.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Eric Renken" <Er*********@newsgroup.nospam> wrote in message
news:uc**************@tk2msftngp13.phx.gbl...
I currently use statements like this in my 1.1 code.

System.Configuration.ConfigurationSettings.AppSett ings[ "jobclock.net" ]

I use this in DLLs that are referenced by the application. The
application has this data in its app.config file. I would like to switch
to the new Setting files in 2.0, but I don't know if I can access these
settings from a different DLL. I can't make a reference in the DLL back
to the application because that would create a circular reference.

Anyone done this or have any ideas?

Thanks,

Eric Renken


Dec 28 '05 #3
Hi Eric,

As far as I know, we cannot get the app setting in the DLL if it is
referenced by an EXE. You have to pass the setting from .EXE to the DLL and
save it in a static variable. When calling the method that needs the
setting, we can pass it as an argument or let the method get directly from
the static variable.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Dec 29 '05 #4
OK, then I really don't like that you are phasing out the old way of doing
this, because with the old way all the DLLs used the config file of the
application that instantiated them when you called
ConfigurationSettings.AppSettings[]. I think this new way has really
limited that. I had some classes that exposed static methods of data stored
int he config file, but those will now have to change because that was in a
common DLL not in the EXE.

Like I have a exception handler that post to a web site and it used to pull
that data out of a config file. Now I have to make sure when the exception
handler gets called that I pass in the web address; where before I could
just pull this once now I have to load it multiple times because I don't
know where it is going to be called from first.

Looks like I will stay with the old way until my code breaks.

I was hoping for a way to call the global:: directly and have to get the
data from the config file.

Eric Renken
"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:bk*************@TK2MSFTNGXA02.phx.gbl...
Hi Eric,

As far as I know, we cannot get the app setting in the DLL if it is
referenced by an EXE. You have to pass the setting from .EXE to the DLL
and
save it in a static variable. When calling the method that needs the
setting, we can pass it as an argument or let the method get directly from
the static variable.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Dec 30 '05 #5
Hi Eric,

Apologize that I have made a mistake in my last post. We can read from an
app.config file from the DLL is it is referenced by an EXE. Because
app.config is per appdomain. If the dll is loaded into the same appdomain
as the exe, it will surely to access the app.config file.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Dec 30 '05 #6
OK, how do I do this? Using the new style, where the data is in a Settings
file (when you make the setting Application specific it stores it in the
app.config)

Eric Renken

"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:$Z**************@TK2MSFTNGXA02.phx.gbl...
Hi Eric,

Apologize that I have made a mistake in my last post. We can read from an
app.config file from the DLL is it is referenced by an EXE. Because
app.config is per appdomain. If the dll is loaded into the same appdomain
as the exe, it will surely to access the app.config file.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 2 '06 #7
Hi Eric,

In .NET 1.1, we use ConfigurationSettings class to get all the settings
from app.config. In .NET 2.0 it's better to use ConfigurationManager class
to achieve this. For example, you have an app.config file like

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="AppSetting0" value="Monday, February 28, 2005 1:27:59 PM" />
<add key="AppSetting1" value="Monday, February 28, 2005 1:36:40 PM" />
</appSettings>
</configuration>

You can use the following to get settings.

public static string m1()
{
return
System.Configuration.ConfigurationManager.AppSetti ngs["AppSetting0"];
}

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 3 '06 #8
OK, ConfigurationManager, thanks.

Eric Renken
"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:wJ**************@TK2MSFTNGXA02.phx.gbl...
Hi Eric,

In .NET 1.1, we use ConfigurationSettings class to get all the settings
from app.config. In .NET 2.0 it's better to use ConfigurationManager class
to achieve this. For example, you have an app.config file like

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="AppSetting0" value="Monday, February 28, 2005 1:27:59 PM" />
<add key="AppSetting1" value="Monday, February 28, 2005 1:36:40 PM" />
</appSettings>
</configuration>

You can use the following to get settings.

public static string m1()
{
return
System.Configuration.ConfigurationManager.AppSetti ngs["AppSetting0"];
}

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 3 '06 #9
You're welcome.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 4 '06 #10
Well this is interesting, because we have a System.Configuration namespace
in System, but to use Configuration you must add the System.Configuration
Reference.

Eric Renken

"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:wJ**************@TK2MSFTNGXA02.phx.gbl...
Hi Eric,

In .NET 1.1, we use ConfigurationSettings class to get all the settings
from app.config. In .NET 2.0 it's better to use ConfigurationManager class
to achieve this. For example, you have an app.config file like

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="AppSetting0" value="Monday, February 28, 2005 1:27:59 PM" />
<add key="AppSetting1" value="Monday, February 28, 2005 1:36:40 PM" />
</appSettings>
</configuration>

You can use the following to get settings.

public static string m1()
{
return
System.Configuration.ConfigurationManager.AppSetti ngs["AppSetting0"];
}

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 4 '06 #11
Hi Eric,

Although System.Configuration namespace is a subnamespace under System,
they are implemented in different assemblies. So if you wish to use the
System.Configuration namespace, you have to add reference to that assembly.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 5 '06 #12
Never had to add it before I started using the new command.

Eric Renken

"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:G6*************@TK2MSFTNGXA02.phx.gbl...
Hi Eric,

Although System.Configuration namespace is a subnamespace under System,
they are implemented in different assemblies. So if you wish to use the
System.Configuration namespace, you have to add reference to that
assembly.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 12 '06 #13
OK... So this is what I did:

- I went to the properties screen of my project and created for
newapplication settings in the 'Settings' tab/section. These are named:
InitalBrowseFolder, ECopyAppPath, RootSaveFolderPath and RootSaveFolder.
-The first three are string types, the last one is of type
System.Environment+SpecialFolder
-All four settings are in the User scope (As I read that settings that are
put in the Application Scope will be read only,correct?)
- I created default values for each setting; I made the first 3 defults c:\
and the last one says MyComputer.

Ok... so I want these settings to persist for the user indefinitely. They
can be changed by the user. The first step if that I want to be able to read
them, and its been really frustrating finding the correct way to do this.
Please improve documentation for this fequently used feature! Anyway...
based on what I've read so far I tried the following:

- I referenced the System.Configuration class, and triues accessing the
ECopyAppPath by using the statement:
ConfigurationManager.AppSettings["ECopyAppPath"] ...it returns null. grrr.

- I also tried instantiating the ApplicationsSettingsBase class:
private ApplicationSettingsBase settings;
...
...
string x = settings.Properties["ECopyAppPath"].ToString();

.... this also returns null.

What am I doing wrong? Are the settings in the wrong place or am I trying
to retrieve them improperly?

"Eric Renken" wrote:
Never had to add it before I started using the new command.

Eric Renken

"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:G6*************@TK2MSFTNGXA02.phx.gbl...
Hi Eric,

Although System.Configuration namespace is a subnamespace under System,
they are implemented in different assemblies. So if you wish to use the
System.Configuration namespace, you have to add reference to that
assembly.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


Jan 13 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.

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.