Properties.Settings [英] Properties.Settings

查看:67
本文介绍了Properties.Settings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用属性设计器来存储应用程序范围的属性时,如何在包含EXE和

DLL集合的项目组中使用它。我正在使用C#.Net 2005.我注意到设计师

创建和app.config被复制到输出目录作为

[programname] .exe .config或[dllname] .dll.config。在此设置中,[programname] .exe.config文件中的任何

应用程序范围属性可以从文件读入并使用
。我的问题是应用程序范围

属性在dll中。即使我将[dllname] .dll.config

文件复制到EXE文件的输出目录中,我也无法从dll中读取任何

应用程序范围参数。我也尝试在[progname] .exe.config文件中设置

设置,这也行不通。 Am

我对任何人都有意义吗?有人有什么建议吗?


Dave

When using the properties designer to store application wide properties how
do you get this to work across a project group containing an EXE and a
collection of DLLs. I''m using C#.Net 2005. I noticed that the designer
creates and app.config that gets copied to the output directory as a
[programname].exe.config or [dllname].dll.config. In this setup any
Application Scope property that is in the [programname].exe.config file can
be read in from the file and used. My problem is with Application Scope
properties that are in the dlls. Even when I copy the [dllname].dll.config
file to the output directory of the EXE file I can never read in any
application scope parameters from within the dlls. I also tried putting
there settings in the [progname].exe.config file, this also didn''t work. Am
I making any sense to anyone? Anyone have any suggestions?

Dave

推荐答案

嗨Dave,


Dll不使用配置文件。如果您计划从dll使用应用程序

设置,则必须将设置添加到引用它的EXE的应用程序

配置文件中。不幸的是,

无法强制你的dll用户在应用程序的配置文件或用户设置文件中提供它所需的设置,所以你必须通过检查属性'

值是否存在来处理该意外事件,并使用硬编码的默认值或者如果它们是

不存在。


2.0框架中的另一种选择是创建一个自定义ASP.NET提供程序

封装设置您的dll需要(提供商基础

在WinForms应用程序中也能很好地工作):


提供者工具包
http://msdn2.microsoft.com/en-us/asp.net/aa336558 .aspx


对于类库,我更喜欢在某些

专有设置文件或应用程序设置中创建自定义提供程序。


-

Dave Sexton
http://davesexton.com/blog

" Dave" < Ki ************ @ community.nospamwrote in message

news:OV ************* @ TK2MSFTNGP04.phx。 gbl ...
Hi Dave,

Dlls do not use configuration files. If you plan to use Application
Settings from a dll then you must add the settings to the application
configuration file of the EXE that is referencing it. Unfortunately, there
is no way to enforce that users of your dll provide the settings that it
expects in the app''s config file or a user settings file, so you have to
handle that contingency by checking for the existence of the properties''
values and either use a hard-coded default or throw an exception if they
aren''t present.

An alternative in the 2.0 framework is to create a custom ASP.NET provider
that encapsulates the settings that your dll needs (the provider foundation
works perfectly well in WinForms applications too):

Provider Toolkit
http://msdn2.microsoft.com/en-us/asp.net/aa336558.aspx

For a class library, I prefer to create a custom provider over some
proprietary settings file or Application Settings in most cases.

--
Dave Sexton
http://davesexton.com/blog

"Dave" <Ki************@community.nospamwrote in message
news:OV*************@TK2MSFTNGP04.phx.gbl...

使用属性设计器存储应用程序范围的属性

如何获得
这适用于包含EXE和

DLL集合的项目组。我正在使用C#.Net 2005.我注意到设计师

创建和app.config被复制到输出目录作为

[programname] .exe .config或[dllname] .dll.config。在此设置中,[programname] .exe.config文件中的任何

应用程序范围属性

可以从文件中读取
和使用。我的问题是应用程序范围

属性在dll中。即使我将

[dllname] .dll.config

文件复制到EXE文件的输出目录中,我也无法读取任何

来自dll内的应用程序范围参数。我也尝试在[progname] .exe.config文件中设置

,这也没有用。

Am

我对任何人都有意义吗?有人有什么建议吗?


Dave

When using the properties designer to store application wide properties
how
do you get this to work across a project group containing an EXE and a
collection of DLLs. I''m using C#.Net 2005. I noticed that the designer
creates and app.config that gets copied to the output directory as a
[programname].exe.config or [dllname].dll.config. In this setup any
Application Scope property that is in the [programname].exe.config file
can
be read in from the file and used. My problem is with Application Scope
properties that are in the dlls. Even when I copy the
[dllname].dll.config
file to the output directory of the EXE file I can never read in any
application scope parameters from within the dlls. I also tried putting
there settings in the [progname].exe.config file, this also didn''t work.
Am
I making any sense to anyone? Anyone have any suggestions?

Dave



Hi Dave,


用于生成DLL的项目类型和

" [dllname] .dll.config"文件?我假设它是类库类型。默认情况下,

类库项目类型不包含Properties.Settings,因此你必须在项目属性对话框中显式创建一个。我假设你已经完成了它。


对不起,我不确定我是否完全了解你。什么是

dlls中的应用程序范围属性意思?你的意思是你在[dllname] .dll.config中创建的

配置值文件?我可以在类库项目中读取它们没有任何问题。以下是我的测试

步骤:


1.我创建一个测试Winform项目并添加一个类库项目

2。我打开类库项目属性对话框并选择设置

选项卡以显式创建[dllname] .dll.config类库的文件

项目。然后我添加了一个应用程序。输入名称为testval的int值

和默认值7。

3.我在类库项目中编写了一个非常简单的静态方法

添加2个输入参数和默认设置值。


命名空间ClassLibrary1

{

公共类Class1

{

public static int Add(int a,int b)

{

返回a + b +

ClassLibrary1.Properties.Settings.Default.testval;

}

}

}


4.在Winform应用程序项目中,我添加了对ClassLibrary1

项目的引用,并编写以下代码来使用它:

private void button1_Click(object sender,EventArgs e)

{

MessageBox.Show(ClassLibrary1.Class1.Add(5,6).ToString());

}


当我跑步并点击按钮时,我可以得到总和值18(5 + 6 + 7)

没有任何问题。如果我误解了你,请随时告诉我

我,谢谢。


祝你好运,

Jeffrey Tan

Microsoft在线社区支持

================================ ==================

通过电子邮件收到我的帖子通知?请参阅
http://msdn.microsoft .com / subscripti ... ult.aspx#notif

ications。


注意:MSDN托管新闻组支持服务是针对非紧急问题

如果社区或微软支持人员在1个工作日内做出初步回复是可以接受的。请注意,每个跟随

的响应可能需要大约2个工作日作为支持

专业人士与您合作可能需要进一步调查才能达到

最有效的分辨率。该产品不适用于需要紧急,实时或基于电话的交互或复杂的b $ b项目分析和转储分析问题的情况。这种性质的问题最好通过联系

Microsoft客户支持服务(CSS)处理
href =http://msdn.microsoft.com/subscriptions/support/default.aspx\"target =_ blank> http://msdn.microsoft.com/subscripti...t/default.aspx

======================================== ==========

此帖子按原样提供。没有保证,也没有授予任何权利。

Hi Dave,

What type of project you are using to generate the DLL and
"[dllname].dll.config" file? I assume it is Class Library type. By default,
Class Library project type does not contain a Properties.Settings, so you
have to explicitly create one in the project property dialog. I assume you
have done it.

Sorry, I am not sure if I understand you completely. What does the
"Application Scope properties that are in the dlls" mean? Do you mean the
config values you created in the "[dllname].dll.config" file? I can read
them without any problem in the Class Library project. Below is my test
steps:

1. I create a test Winform project and add a Class Library project
2. I open the class library project property dialog and choose "Settings"
tab to explicitly create the "[dllname].dll.config" file for Class Library
project. Then I added a "Application" type int value with name "testval"
and default value "7".
3. I write a very simple static method in the Class Library project which
add 2 input parameters and the default setting value.

namespace ClassLibrary1
{
public class Class1
{
public static int Add(int a, int b)
{
return a + b +
ClassLibrary1.Properties.Settings.Default.testval;
}
}
}

4. In Winform application project I add the reference to "ClassLibrary1"
project and write the following code to use it:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(ClassLibrary1.Class1.Add(5, 6).ToString());
}

When I run and click the button, I can get the sum value of "18"(5+6+7)
without any problem. If I have misunderstood you, please feel free to tell
me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


您好Jeffery,


应用程序设置可能会保留在你所描述的类

库的配置文件,但是,值得注意的是框架不会加载任何其他配置元素

班级

库的配置文件。所有其他配置设置必须位于EXE的

配置文件中,因此应用程序按预期运行需要两个.config文件。出于这个原因,我更喜欢

使用可以在一个地方注册的提供商(EXE的配置)

为a提供可配置的设置类库。


使用2.0 Provider框架,您将使用一个抽象类,它将提供一定程度的编译时检查以确保属性

和为类库提供设置的方法至少是由应用程序实现的
。从这里,应用程序的作者

可以选择是否使用应用程序来保留*提供的*属性

EXE的设置或仅使用提供程序设置,这将使更多

感觉在这种情况下,IMO。这是正常配置设置的一个配置文件,

提供商设置,甚至包含应用程序范围的应用程序设置。


自定义提供商之间提供合同应用程序和类

库因此更灵活,只需要一个配置

文件。虽然,使用应用程序设置肯定要简单得多,并且

将节省时间:)


-

Dave Sexton http://davesexton.com/blog


"" Jeffrey Tan [MSFT]"" < je *** @ online.microsoft.com写信息

新闻:2i ************** @ TK2MSFTNGHUB02.phx.gbl ...
Hi Jeffery,

Application Settings may be persisted in a configuration file for a class
library as you''ve described, however, it''s probably worth noting that no
other configuration elements will be loaded by the framework from the class
library''s config file. All other configuration settings must be in the
configuration file of the EXE, so two .config files would be required for
the application to run as expected. For this reason, among others, I prefer
to use a provider that can be registered in one place (the EXE''s config)
when providing configurable settings for a class library.

With the 2.0 Provider framework you would use an abstract class, which will
provide some level of compile-time checking to ensure that the properties
and methods that provide settings for the class library are at least
implemented by the application. From here, the author of the application
can choose whether to persist the *provided* properties using Application
Settings for the EXE or just use provider settings, which would make more
sense in this case, IMO. That''s one config file for normal config settings,
provider settings and even Application Settings with application-scope.

A custom provider provides a contract between the application and the class
library and therefore is more flexible, and only requires a single config
file. Although, using Application Settings is certainly much simpler and
will save time :)

--
Dave Sexton
http://davesexton.com/blog

""Jeffrey Tan[MSFT]"" <je***@online.microsoft.comwrote in message
news:2i**************@TK2MSFTNGHUB02.phx.gbl...

嗨Dave,


用于生成DLL的项目类型和

" [其中dllname] .dll.config"文件?我假设它是类库类型。通过

默认,

类库项目类型不包含Properties.Settings,所以你要
必须在project属性中显式创建一个对话。我假设你已经完成了它。


对不起,我不确定我是否完全了解你。什么是

dlls中的应用程序范围属性意思?你的意思是你在[dllname] .dll.config中创建的

配置值文件?我可以在类库项目中读取它们没有任何问题。以下是我的测试

步骤:


1.我创建一个测试Winform项目并添加一个类库项目

2。我打开类库项目属性对话框并选择设置

选项卡以显式创建[dllname] .dll.config类库的文件

项目。然后我添加了一个应用程序。输入名称为testval的int值

和默认值7。

3.我在类库项目中编写了一个非常简单的静态方法

添加2个输入参数和默认设置值。


命名空间ClassLibrary1

{

公共类Class1

{

public static int Add(int a,int b)

{

返回a + b +

ClassLibrary1.Properties.Settings.Default.testval;

}

}

}


4.在Winform应用程序项目中,我添加了对ClassLibrary1

项目的引用,并编写以下代码来使用它:

private void button1_Click(object sender,EventArgs e)

{

MessageBox.Show(ClassLibrary1.Class1.Add(5,6).ToString());

}


当我跑步并点击按钮时,我可以得到总和值18(5 + 6 + 7)

没有任何问题。如果我误解了你,请随时告诉我

我,谢谢。


祝你好运,

Jeffrey Tan

Microsoft在线社区支持

================================ ==================

通过电子邮件收到我的帖子通知?请参阅
http://msdn.microsoft .com / subscripti ... ult.aspx#notif

ications。


注意:MSDN托管新闻组支持服务是针对非紧急问题

如果社区或微软支持人员在1个工作日内做出初步回复是可以接受的。请注意,每个跟随

的响应可能需要大约2个工作日作为支持

专业人士与您合作可能需要进一步调查才能达到

最有效的分辨率。该产品不适用于需要紧急,实时或基于电话的交互或复杂的b $ b项目分析和转储分析问题的情况。这种性质的问题最好通过联系

Microsoft客户支持服务(CSS)处理
href =http://msdn.microsoft.com/subscriptions/support/default.aspx\"target =_ blank> http://msdn.microsoft.com/subscripti...t/default.aspx

======================================== ==========

此帖子按原样提供。没有保证,也没有赋予

权利。
Hi Dave,

What type of project you are using to generate the DLL and
"[dllname].dll.config" file? I assume it is Class Library type. By
default,
Class Library project type does not contain a Properties.Settings, so you
have to explicitly create one in the project property dialog. I assume you
have done it.

Sorry, I am not sure if I understand you completely. What does the
"Application Scope properties that are in the dlls" mean? Do you mean the
config values you created in the "[dllname].dll.config" file? I can read
them without any problem in the Class Library project. Below is my test
steps:

1. I create a test Winform project and add a Class Library project
2. I open the class library project property dialog and choose "Settings"
tab to explicitly create the "[dllname].dll.config" file for Class Library
project. Then I added a "Application" type int value with name "testval"
and default value "7".
3. I write a very simple static method in the Class Library project which
add 2 input parameters and the default setting value.

namespace ClassLibrary1
{
public class Class1
{
public static int Add(int a, int b)
{
return a + b +
ClassLibrary1.Properties.Settings.Default.testval;
}
}
}

4. In Winform application project I add the reference to "ClassLibrary1"
project and write the following code to use it:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(ClassLibrary1.Class1.Add(5, 6).ToString());
}

When I run and click the button, I can get the sum value of "18"(5+6+7)
without any problem. If I have misunderstood you, please feel free to tell
me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.



这篇关于Properties.Settings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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