C# vs中应用设置变量如何绑定及代码中如何使用
来源:互联网 发布:abap 修改内表数据 编辑:程序博客网 时间:2024/05/21 10:27
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 Name
property.
Application settings can be stored as any data type that can be serialized to XML or has a TypeConverter
that 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.
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.
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 SpecialSettings
class is not exposed in the My
namespace, 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.Settings
object. On the Settings page, click the View code button to view the Settings.vb file. Settings.vb defines the Settings
class, which enables you to handle these events on the settings class: SettingChanging, PropertyChanged, SettingsLoaded, and SettingsSaving. Notice that the Settings
class 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.Settings
object, 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.Settings
object 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 MySettings
class 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 Settings
class directly, as shown in the following Visual C# example.
Properties.Settings.Default.FirstUserSetting = "abc";
You must also explicitly call the Save
method of this wrapper class in order to persist the user settings. You usually do this in the Closing
event handler of the main form. The following Visual C# example shows a call to the Save
method.
Properties.Settings.Default.Save();
For general information about accessing application settings through the Settings
class, see Application Settings Overview. For information about iterating through the settings, see this forum post.
- C# vs中应用设置变量如何绑定及代码中如何使用
- C#中如何使用static静态变量
- VS中如何设置自己的快捷代码段
- js中函数、变量如何在C#中使用?C#中函数、变量如何在js中使用?
- VS中如何使用SQLite(C#版)
- 如何在vs studio中使用代码云托管
- VS 2005 中如何设置默认浏览器
- xpath中如何使用变量
- 在代码中如何获取应用签名及sha1值
- STM32 中,如何设置一个位变量?
- C# 程序中如何向(DEV)ComboBoxEdit控件中添加下拉列表的值不使用数据绑定,就是使用代码添加
- iOS应用中如何设置使用的语言
- c# WinForm中如何在代码中设置控件的padding 设置Label的字体
- c# WinForm中如何在代码中设置控件的padding 设置Label的字体
- 在WPF中如何使用RelativeSource绑定
- 如何在VS2005中使用C#开发Silverlight 1.1应用
- 如何获取Oracle share pool中没有使用绑定变量的SQL
- 如何监控并找出系统中存在的大量的未使用绑定变量的SQL
- 2017-03-18 Ice_cream’s world II
- g++ gdb
- Java代码6种方法获取spring 容器的bean
- 初识文件系统
- java运算符
- C# vs中应用设置变量如何绑定及代码中如何使用
- LoRaWAN协议中文版 第3章 PHY帧格式
- 剑指offer--赋值运算符函数
- 欢迎使用CSDN-markdown编辑器
- 三、带壳密钥破解【GetStartupInfo(取初始断点)--->(CTRL+G 401000--->(关键字)--->(Software\engydt 下段))】
- 使用Dir函数遍历文件和目录
- nginx代码学习wiki-基本数据结构
- linux服务器之间免秘钥互相访问
- 360在线笔试编程题2018