C# vs中应用设置变量如何绑定及代码中如何使用
2017-03-18 21:35
936 查看
For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.
Application settings enable you to store application information dynamically. Settings allow you to store information on the client computer that should not be included in the application code (for example a connection string), user preferences and other information
you need at runtime.
Application settings replace the dynamic properties used in earlier versions of Visual Studio.
Each application setting must have a unique name. The name can be any combination of letters, numbers, or an underscore that does not start with a number, and it cannot contain spaces. The name can be changed through the
Application settings can be stored as any data type that can be serialized to XML or has a
but you can also store values as Color, Object,
or as a connection string.
Application settings also contain a value. The value is set with the Value property and must match the data type of the setting.
In addition, application settings can be bound to a property of a form or control at design time.
There are two types of application settings, based on scope:
Application-scoped settings can be used for information such as a URL for a Web service or a database connection string. These values are associated with the application. Therefore, users cannot change them at run time.
User-scoped settings can be used for information such as persisting the last position of a form or a font preference. Users can change these values at run time.
You can change the type of a setting by using the Scope property.
The project system stores application settings in two XML files: an app.config file, which is created at design time when you create the first application setting; and a user.config file, which is created at run time when the user who runs the application changes
the value of any user setting. Notice that changes in user settings are not written to disk unless the application specifically calls a method to do this.
At design time, you can create application settings in two ways: by using the Settings page of the Project Designer, or by using the Properties window for a form or control, which allows you to bind a setting
to a property.
When you create an application-scoped setting (for example, a database connection string, or a reference to server resources), Visual Studio saves it in app.config with the
When you create a user-scoped setting (for example, default font, home page, or window size), Visual Studio saves it in app.config with the
You can add customized settings files to your project for convenient management of groups of settings. Settings that are contained in a single file are loaded and saved as a unit. Therefore, being able to store settings in separate files for frequently-used
and infrequently-used groups can save time in loading and saving settings.
For example, you can add a file such as SpecialSettings.settings to your project. While your
The Settings Designer first searches for the Settings.settings file that the project system creates; this is the default file that the Project Designer displays in the Settings tab. Settings.settings is located in the My Project folder for
Visual Basic projects and in the Properties folder for Visual C# projects. The Project Designer then searches for other settings files in the project's root folder. Therefore, you should put your custom settings file there. If you add a .settings file elsewhere
in your project, the Project Designer will not be able to locate it.
In Visual Basic projects, you can access application settings at run time by using the
which enables you to handle these events on the settings class: SettingChanging, PropertyChanged, SettingsLoaded,
and SettingsSaving. Notice that the
is a partial class that displays only the user-owned code, not the whole generated class. For more information about accessing application settings by using the
Application Settings.
The values of any user-scoped settings that the user changes at run time (for example, the position of a form) are stored in a user.config file. Notice that the default values are still saved in app.config.
If you have changed any user-scoped settings during run time, for example in testing the application, and want to reset these settings to their default values, click the Synchronize button.
We strongly recommend that you use the
saved before application shutdown. However, your Visual Basic application can access settings directly. In that case you have to access the
settings before ending the application, as you would do for a C# application; this is described in the following section.
In languages other than Visual Basic, such as Visual C#, you must access the
C#
You must also explicitly call the
C#
For general information about accessing application settings through the
Overview. For information about iterating through the settings, see this forum
post.
Application settings enable you to store application information dynamically. Settings allow you to store information on the client computer that should not be included in the application code (for example a connection string), user preferences and other information
you need at runtime.
Application settings replace the dynamic properties used in earlier versions of Visual Studio.
Each application setting must have a unique name. The name can be any combination of letters, numbers, or an underscore that does not start with a number, and it cannot contain spaces. The name can be changed through the
Nameproperty.
Application settings can be stored as any data type that can be serialized to XML or has a
TypeConverterthat implements
ToString/
FromString. The most common types are
String,
Integer, and
Boolean,
but you can also store values as Color, Object,
or as a connection string.
Application settings also contain a value. The value is set with the Value property and must match the data type of the setting.
In addition, application settings can be bound to a property of a form or control at design time.
There are two types of application settings, based on scope:
Application-scoped settings can be used for information such as a URL for a Web service or a database connection string. These values are associated with the application. Therefore, users cannot change them at run time.
User-scoped settings can be used for information such as persisting the last position of a form or a font preference. Users can change these values at run time.
You can change the type of a setting by using the Scope property.
The project system stores application settings in two XML files: an app.config file, which is created at design time when you create the first application setting; and a user.config file, which is created at run time when the user who runs the application changes
the value of any user setting. Notice that changes in user settings are not written to disk unless the application specifically calls a method to do this.
Creating Application Settings
at Design Time
At design time, you can create application settings in two ways: by using the Settings page of the Project Designer, or by using the Properties window for a form or control, which allows you to bind a setting
to a property.
When you create an application-scoped setting (for example, a database connection string, or a reference to server resources), Visual Studio saves it in app.config with the
<applicationSettings>tag. (Connection strings are saved under the
<connectionStrings>tag.)
When you create a user-scoped setting (for example, default font, home page, or window size), Visual Studio saves it in app.config with the
<userSettings>tag.
Important |
---|
When you store connection strings in app.config, you should take precautions to avoid revealing sensitive information, such as passwords or server paths, in the connection string. If you take connection string information from an external source, such as a user supplying a user ID and password, you must be careful to ensure that the values that you use to construct your connection string do not contain additional connection string parameters that change the behavior of your connection. Consider using the Protected Configuration feature to encrypt sensitive information in the configuration file. See Protecting Connection Information for more information. |
Note |
---|
Because there is no configuration file model for class libraries, application settings do not apply for Class Library projects. The exception is a Visual Studio Tools for Office DLL project, which can have a configuration file. |
Using Customized Settings
Files
You can add customized settings files to your project for convenient management of groups of settings. Settings that are contained in a single file are loaded and saved as a unit. Therefore, being able to store settings in separate files for frequently-used
and infrequently-used groups can save time in loading and saving settings.
For example, you can add a file such as SpecialSettings.settings to your project. While your
SpecialSettingsclass is not exposed in the
Mynamespace, View Code can read the custom settings file that contains
Partial Class SpecialSettings.
The Settings Designer first searches for the Settings.settings file that the project system creates; this is the default file that the Project Designer displays in the Settings tab. Settings.settings is located in the My Project folder for
Visual Basic projects and in the Properties folder for Visual C# projects. The Project Designer then searches for other settings files in the project's root folder. Therefore, you should put your custom settings file there. If you add a .settings file elsewhere
in your project, the Project Designer will not be able to locate it.
Accessing or Changing Application
Settings at Run Time in Visual Basic
In Visual Basic projects, you can access application settings at run time by using the
My.Settingsobject. On the Settings page, click the View code button to view the Settings.vb file. Settings.vb defines the
Settingsclass,
which enables you to handle these events on the settings class: SettingChanging, PropertyChanged, SettingsLoaded,
and SettingsSaving. Notice that the
Settingsclass in Settings.vb
is a partial class that displays only the user-owned code, not the whole generated class. For more information about accessing application settings by using the
My.Settingsobject, see Accessing
Application Settings.
The values of any user-scoped settings that the user changes at run time (for example, the position of a form) are stored in a user.config file. Notice that the default values are still saved in app.config.
If you have changed any user-scoped settings during run time, for example in testing the application, and want to reset these settings to their default values, click the Synchronize button.
We strongly recommend that you use the
My.Settingsobject and the default .settings file to access settings. This is because you can use the Settings Designer to assign properties to settings, and, additionally, user settings are automatically
saved before application shutdown. However, your Visual Basic application can access settings directly. In that case you have to access the
MySettingsclass and use a custom .settings file in the root of the project. You must also save the user
settings before ending the application, as you would do for a C# application; this is described in the following section.
Accessing or Changing Application
Settings at Run Time in Visual C#
In languages other than Visual Basic, such as Visual C#, you must access the
Settingsclass directly, as shown in the following Visual C# example.
C#
Properties.Settings.Default.FirstUserSetting = "abc";
You must also explicitly call the
Savemethod of this wrapper class in order to persist the user settings. You usually do this in the
Closingevent handler of the main form. The following Visual C# example shows a call to the
Savemethod.
C#
Properties.Settings.Default.Save();
For general information about accessing application settings through the
Settingsclass, see Application Settings
Overview. For information about iterating through the settings, see this forum
post.
相关文章推荐
- 【C#语言学习】之如何在VS2015中使用git管理代码
- C# 程序中如何向(DEV)ComboBoxEdit控件中添加下拉列表的值不使用数据绑定,就是使用代码添加
- 在C#代码中应用Log4Net(一)简单使用Log4Net
- 如何绑定变量使用
- 使用Object.defineProperty如何巧妙找到修改某个变量的准确代码位置
- VS2005里如何用WSE产生WSDL的C#代码
- 如何使用WebBrowser控件打印格式化的XML文档,并以编程方式任意设置打印方向(C#完整示例)
- 如何使用VS发布ASP.NET应用到Windows Azure
- javascript代码里绑定c#变量
- vs.net C#对各类数据库使用变量的方式
- 黄聪:如何用代码设置控制自己网站的网页在360浏览器打开时强制优先使用极速模式,而非兼容模式
- VS2010 C#如何设置使用DirectX
- android 如何使用命令和代码设置heapsize
- C#中如何定义全局变量及在各窗体中使用全局变量
- 如何在Java代码中使用SAP云平台CloudFoundry环境的环境变量
- 如何统计网站访问量,代码实现使用全局变量 Application,start,end,Session_start,Session_end事件
- 如何使用C#在发送往client的内容上加js代码(转)
- AutoCode (vs插件)自动生成代码插件 下载及使用教程(C#版)
- 使用VS2013自带的PreEmptive Dotfuscator and Analytis来混淆C#代码
- 如何使用vs调试.net framework代码 (一)