程序集的App设置。 [英] App Settings for assemblies.

查看:60
本文介绍了程序集的App设置。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.exe控制台项目和一个类库项目作为

解决方案的一部分。 .exe有一个App.config文件,我使用了设置

页面来添加一些应用程序级别设置。


当我尝试检索设置时使用此代码:


//方法1


string s = ConfigurationManager.AppSettings [" TestSetting"];


我什么也得不回来。但如果我使用以下代码:


//方法2


Properties.Settings _settings = new

ThrowAwayCs.Properties.Settings();

s = _settings.TestSetting;


我得到了正确的值。我喜欢第二种方法,因为我得到了所有设置的
intellisense。如果我在代码中输入错误,第一种方法似乎容易出现错误
错误,因为我不知道它直到运行时才会知道它。


我有两个问题:


1.这种方法(方法2)是否使用了Settings类(在Settings.Designer中定义了

) .cs)认为合适吗?好像

工作正常。


2.我想在类库项目中使用第二种方法

但是由于应用程序引用了类库,因为一个循环的

引用,我无法从类库中引用应用程序。我想在我的设置中有完整的intellisense

类libaray。这可能吗?


我将不胜感激任何其他人如何使用应用程序的评论

来自应用程序引用的类库的设置。


谢谢,


克里斯

关键词:

dll

app

设置

参考

intellisense

类库

解决方案

你好Chris,


我明白这一点有点令人困惑,因为当你使用
$时b $ b创建IDE中的设置页面,命名空间(属性)和一组静态

类(设置和资源),您可以在其中看到你的

项目通过Class Viewer。


它实际上要简单得多,因为每个项目都有自己的类,如果你是

有一个类库,它将使用应用程序的Settings

文件加载Properties类类库。这样,每个应用程序

可以使用相同的类库具有不同的配置设置。

设置类有一个名为默认的静态设置成员。

将按名称返回配置文件中的所有属性。示例:


AppLogFilePath = Properties.Settings.Default.AppLogFilePath;


因此,例如,您可以创建一个类库,自己在IDE中设置了
,这样你就可以在开发时使用Intellisense了,然后用
创建多个带有自己设置的应用程序(名称与
类Libary的设置,类库将使用应用程序的

设置。


你也可以创建应用程序和用户特定设置,

用户特定设置可以保存回用户的本地设置文件

(位于Documents and Settings / userName / Local Settings / Application

Data)。


参见 http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx 更详细

信息。


-

HTH,


Kevin Spencer

Microsoft MVP

软件编写器
http://unclechutney.blogspot.com

观看时钟永不沸腾。


Chris Dunaway < du ****** @ gmail.com写信息

news:11 ********************* @ m73g2000cwd。 googlegro ups.com ...


>我有一个.exe控制台项目和一个类库项目作为

解决方案的一部分。 .exe有一个App.config文件,我使用了设置

页面来添加一些应用程序级别设置。


当我尝试检索设置时使用此代码:


//方法1


string s = ConfigurationManager.AppSettings [" TestSetting"];


我什么也得不回来。但如果我使用以下代码:


//方法2


Properties.Settings _settings = new

ThrowAwayCs.Properties.Settings();

s = _settings.TestSetting;


我得到了正确的值。我喜欢第二种方法,因为我得到了所有设置的
intellisense。如果我在代码中输入错误,第一种方法似乎容易出现错误
错误,因为我不知道它直到运行时才会知道它。


我有两个问题:


1.这种方法(方法2)是否使用了Settings类(在Settings.Designer中定义了

) .cs)认为合适吗?好像

工作正常。


2.我想在类库项目中使用第二种方法

但是由于应用程序引用了类库,因为一个循环的

引用,我无法从类库中引用应用程序。我想在我的设置中有完整的intellisense

类libaray。这可能吗?


我将不胜感激任何其他人如何使用应用程序的评论

来自应用程序引用的类库的设置。


谢谢,


克里斯


关键词:

dll

app

设置

参考

intellisense

类库


Kevin Spencer写道:


谢谢,你已经清理了很多。


因此,例如,您可以创建一个类库,在IDE中使用自己的设置,以便在开发它时使用智能感知器,并且

然后使用他们自己的设置创建多个应用程序(名称与

类Libary'的设置相同,类库将使用应用程序的

设置。



所以我添加到类库的Properties页面的任何设置

项目,最终必须放在MyApp.Exe.Config文件中,

正确吗?当您只分发类库时,将这些设置纳入应用程序的配置文件的最佳做法是什么?

是否有任何方法可以将设置集成到现有

app.config?


此外,如果主机应用程序的参数名称与一个

一样,该怎么办?你的班级图书馆?是否有任何建议的方法来命名

您的设置以避免名称冲突?


您还可以创建应用程序和用户特定设置,以及

用户专用设置可以保存回用户的本地设置文件

(位于Documents and Settings / userName / Local Settings / Application
数据)。



我想知道那是哪里!


再次感谢,


克里斯


凯文 -


我想在VB.NET中这样做,我''我无法使它工作。我尝试过以下各种变化。

前两个向我显示了

IntelliSense中的正确设置,但在运行时抛出异常(对象引用未设置为

对象的实例。) 。第三个没有编译,因为DBServerName是

不是默认的公认成员:

sServerName =

My.MySettings.Default。 Properties.Item(DBServerNam e)。ToString()

sServerName = My.MySettings.Default.Item(" DBServerName")。ToStrin g()

sServerName = My.MySettings.Default.DBServerName


我需要做些什么才能将应用程序设置暴露给

程序集?


提前致谢。


" Kevin Spencer"写道:


你好Chris,


我明白这一点有点令人困惑,因为当你使用

创建IDE中的设置页面,命名空间(属性)和一组静态

类(设置和资源),其中你可以通过Class Viewer在你的
项目中看到。


它实际上要简单得多,因为每个项目都有自己的类,如果你是/>
有一个类库,它将使用Class libary从应用程序的Settings

文件加载Properties类。这样,每个应用程序

可以使用相同的类库具有不同的配置设置。

设置类有一个名为默认的静态设置成员。

将按名称返回配置文件中的所有属性。示例:


AppLogFilePath = Properties.Settings.Default.AppLogFilePath;


因此,例如,您可以创建一个类库,自己在IDE中设置了
,这样你就可以在开发时使用Intellisense了,然后用
创建多个带有自己设置的应用程序(名称与
类Libary的设置,类库将使用应用程序的

设置。


你也可以创建应用程序和用户特定设置,

用户特定设置可以保存回用户的本地设置文件

(位于Documents and Settings / userName / Local Settings / Application

Data)。


参见 http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx 更详细

信息。


-

HTH,


Kevin Spencer

Microsoft MVP

软件编写器
http://unclechutney.blogspot.com

观看时钟永不沸腾。


" ; Chris Dunaway < du ****** @ gmail.com写信息

news:11 ********************* @ m73g2000cwd。 googlegro ups.com ...


我有一个.exe控制台项目和一个类库项目作为

解决方案的一部分。 .exe有一个App.config文件,我使用了设置

页面来添加一些应用程序级别设置。


当我尝试检索设置时使用此代码:


//方法1


string s = ConfigurationManager.AppSettings [" TestSetting"];


我什么也得不回来。但如果我使用以下代码:


//方法2


Properties.Settings _settings = new

ThrowAwayCs.Properties.Settings();

s = _settings.TestSetting;


我得到了正确的值。我喜欢第二种方法,因为我得到了所有设置的
intellisense。如果我在代码中输入错误,第一种方法似乎容易出现错误
错误,因为我不知道它直到运行时才会知道它。


我有两个问题:


1.这种方法(方法2)是否使用了Settings类(在Settings.Designer中定义了

) .cs)认为合适吗?好像

工作正常。


2.我想在类库项目中使用第二种方法

但是由于应用程序引用了类库,因为一个循环的

引用,我无法从类库中引用应用程序。我想在我的设置中有完整的intellisense

类libaray。这可能吗?


我将不胜感激任何其他人如何使用应用程序的评论

来自应用程序引用的类库的设置。


谢谢,


克里斯

关键词:

dll

app

设置

参考

intellisense

类库




I have an .exe console project and a class library project as part of a
solution. The .exe has an App.config file and I have used the Settings
page to add some Application level settings.

When I try to retrieve a setting using this code:

//Method 1

string s = ConfigurationManager.AppSettings["TestSetting"];

I don''t get anything back. But if I use the following code:

//Method 2

Properties.Settings _settings = new
ThrowAwayCs.Properties.Settings();
s = _settings.TestSetting;

I do get the correct value. I like the second method because I get
intellisense for all the settings. The first method seems prone to
error if I have a typo in the code because I will not know about it
until runtime.

I have two questions:

1. Is this way (method 2) of using the Settings class (which is
defined in Settings.Designer.cs) considered appropriate? It seems to
work fine.

2. I would like to use the second method in a class library project
but since the application references the class library, I cannot
reference the app from the class library because of a circular
reference. I would like to have full intellisense for my settings
inside the class libaray. Is this possible?

I would appreciate any comments on how others have used Application
Settings from a class library that is referenced by the app.

Thanks,

Chris
Keywords:
dll
app
settings
reference
intellisense
class library

解决方案

Hi Chris,

I understand that this is a bit confusing at first, because when you use the
Settings Page in the IDE, a namespace ("Properties") and a set of static
classes ("Settings" and "Resources") is created, which you can see in your
project via the Class Viewer.

It is actually much simpler, as each project has its own class, and if you
have a class library, it will load the Properties class from the Settings
file of the application using the Class libary. This way, each application
can have different configured Settings using the same Class Library. The
Settings class has a static Settings member named "Default" which will
return all of the Properties in the configuration file by name. Example:

AppLogFilePath = Properties.Settings.Default.AppLogFilePath;

So, for example, you can create a Class Library, with its own Settings in
the IDE, so that you will have the Intellisense while developing it, and
then create multiple apps with their own Settings (with the same names as
the Class Libary''s Settings, and the Class Library will use the app''s
Settings.

You can also create Application and User-Specific Settings, and the
User-Specific Settings can be saved back to the user''s local Settings file
(located in Documents and Settings/userName/Local Settings/Application
Data).

See http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx for more detailed
information.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.

"Chris Dunaway" <du******@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...

>I have an .exe console project and a class library project as part of a
solution. The .exe has an App.config file and I have used the Settings
page to add some Application level settings.

When I try to retrieve a setting using this code:

//Method 1

string s = ConfigurationManager.AppSettings["TestSetting"];

I don''t get anything back. But if I use the following code:

//Method 2

Properties.Settings _settings = new
ThrowAwayCs.Properties.Settings();
s = _settings.TestSetting;

I do get the correct value. I like the second method because I get
intellisense for all the settings. The first method seems prone to
error if I have a typo in the code because I will not know about it
until runtime.

I have two questions:

1. Is this way (method 2) of using the Settings class (which is
defined in Settings.Designer.cs) considered appropriate? It seems to
work fine.

2. I would like to use the second method in a class library project
but since the application references the class library, I cannot
reference the app from the class library because of a circular
reference. I would like to have full intellisense for my settings
inside the class libaray. Is this possible?

I would appreciate any comments on how others have used Application
Settings from a class library that is referenced by the app.

Thanks,

Chris
Keywords:
dll
app
settings
reference
intellisense
class library



Kevin Spencer wrote:

Thanks, you have cleared up much.

So, for example, you can create a Class Library, with its own Settings in
the IDE, so that you will have the Intellisense while developing it, and
then create multiple apps with their own Settings (with the same names as
the Class Libary''s Settings, and the Class Library will use the app''s
Settings.

So any settings I add to the Properties page for the class library
project, will ultimately have to be put in the MyApp.Exe.Config file,
correct? What are the best practices for getting these settings into
the config file for the app when you distribute just the class library?
Are there any methods for integrating the settings into the existing
app.config?

Also, what if the host app has a parameter with the same name as one
used by your class library? Are there any recommended ways of naming
your settings to avoid name collisions?

You can also create Application and User-Specific Settings, and the
User-Specific Settings can be saved back to the user''s local Settings file
(located in Documents and Settings/userName/Local Settings/Application
Data).

I was wondering where that was!

Thanks again,

Chris


Kevin -

I''m trying to do this in VB.NET and I''m unable to get it working. I''ve tried
the following variations; the first two show me the proper setting in
IntelliSense, but throw an exception at runtime ("Object reference not set to
an instance of an object."). The third one doesn''t compile as DBServerName is
not a recognized member of Default:
sServerName =
My.MySettings.Default.Properties.Item("DBServerNam e").ToString()
sServerName = My.MySettings.Default.Item("DBServerName").ToStrin g()
sServerName = My.MySettings.Default.DBServerName

Is there something more I need to do to expose application settings to the
assemblies?

Thanks in advance.

"Kevin Spencer" wrote:

Hi Chris,

I understand that this is a bit confusing at first, because when you use the
Settings Page in the IDE, a namespace ("Properties") and a set of static
classes ("Settings" and "Resources") is created, which you can see in your
project via the Class Viewer.

It is actually much simpler, as each project has its own class, and if you
have a class library, it will load the Properties class from the Settings
file of the application using the Class libary. This way, each application
can have different configured Settings using the same Class Library. The
Settings class has a static Settings member named "Default" which will
return all of the Properties in the configuration file by name. Example:

AppLogFilePath = Properties.Settings.Default.AppLogFilePath;

So, for example, you can create a Class Library, with its own Settings in
the IDE, so that you will have the Intellisense while developing it, and
then create multiple apps with their own Settings (with the same names as
the Class Libary''s Settings, and the Class Library will use the app''s
Settings.

You can also create Application and User-Specific Settings, and the
User-Specific Settings can be saved back to the user''s local Settings file
(located in Documents and Settings/userName/Local Settings/Application
Data).

See http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx for more detailed
information.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.

"Chris Dunaway" <du******@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...

I have an .exe console project and a class library project as part of a
solution. The .exe has an App.config file and I have used the Settings
page to add some Application level settings.

When I try to retrieve a setting using this code:

//Method 1

string s = ConfigurationManager.AppSettings["TestSetting"];

I don''t get anything back. But if I use the following code:

//Method 2

Properties.Settings _settings = new
ThrowAwayCs.Properties.Settings();
s = _settings.TestSetting;

I do get the correct value. I like the second method because I get
intellisense for all the settings. The first method seems prone to
error if I have a typo in the code because I will not know about it
until runtime.

I have two questions:

1. Is this way (method 2) of using the Settings class (which is
defined in Settings.Designer.cs) considered appropriate? It seems to
work fine.

2. I would like to use the second method in a class library project
but since the application references the class library, I cannot
reference the app from the class library because of a circular
reference. I would like to have full intellisense for my settings
inside the class libaray. Is this possible?

I would appreciate any comments on how others have used Application
Settings from a class library that is referenced by the app.

Thanks,

Chris
Keywords:
dll
app
settings
reference
intellisense
class library




这篇关于程序集的App设置。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆