In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to operate the .net Framework configuration file. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
The application of .NET Framework can help developers create a platform that supports the deployment of WEB applications. It is convenient for programmers to write code. We will give you a detailed introduction to the operation of the .NET Framework configuration file in this article, which we hope will be helpful to you.
.net Framework configuration file 1. Create a configuration section class
Objects inherited from ConfigurationSection must be created to read and write configuration data. ConfigurationSection provides indexers to get and set configuration data. It is important to note that only properties with ConfigurationProperty properties are stored, and the names must be exactly the same case, as in the following code, all "id" must remain the same.
Class ConfigSectionData:
ConfigurationSection
{
[ConfigurationProperty ("id")]
Public int Id
{
Get {return (int) this ["id"]
}
Set {this ["id"] = value
}
}
[ConfigurationProperty ("time")]
Public DateTime Time
{
Get
{
Return (DateTime) this ["time"]
}
Set {this ["time"] = value
}
}
}
.net Framework configuration file 2. Create profile Operand
Configuration config =
ConfigurationManager.
OpenExeConfiguration
(ConfigurationUserLevel.None)
ConfigSectionData data =
New ConfigSectionData ()
Data.Id = 1000; data.Time =
DateTime.Now
Config.Sections.Add ("add", data)
Config.Save (Configuration
SaveMode.Minimal)
The above example is the operation app.config, which writes configuration data named "add" under the root node (configuration).
Note that VS2005 writes information to * .vshost.exe.config in IDE mode and overwrites the file when the program closes, so you may not see the configuration data you wrote, as long as you execute the * .exe file in resource management, you can see the result in the * .config file.
If we need to manipulate non-default configuration files, we can use the ExeConfigurationFileMap object.
ExeConfigurationFileMap
File = new ExeConfigurationFileMap ()
File.ExeConfigFilename = "test.config"
Configuration config = ConfigurationManager.
OpenMappedExeConfiguration (file
ConfigurationUserLevel.None)
ConfigSectionData data =
New ConfigSectionData ()
Data.Id = 1000; data.Time = DateTime.Now
Config.Sections.Add ("add", data)
Config.Save (ConfigurationSaveMode.Minimal)
If we do not want to write configuration data under the root node, we can use the ConfigurationSectionGroup object.
ExeConfigurationFileMap
File = new ExeConfigurationFileMap ()
File.ExeConfigFilename = "test.config"
Configuration config = ConfigurationManager.
OpenMappedExeConfiguration (file
ConfigurationUserLevel.None)
ConfigSectionData data =
New ConfigSectionData (); data.Id = 1000
Data.Time = DateTime.Now
Config.SectionGroups.Add ("group1"
New ConfigurationSectionGroup ()
Config.SectionGroups ["group1"].
Sections.Add ("add", data)
Config.Save (ConfigurationSaveMode.Minimal)
Here is the generated configuration file.
.net Framework configuration file 3. Read configuration file
ExeConfigurationFileMap
File = new ExeConfigurationFileMap ()
File.ExeConfigFilename = "test.config"
Configuration config = ConfigurationManager.
OpenMappedExeConfiguration (file
ConfigurationUserLevel.None)
ConfigSectionData data = config.
SectionGroups ["group1"].
Sections ["add"] as ConfigSectionData
/ / ConfigSectionData data =
Config.Sections ["add"] as
ConfigSectionData
/ / read from the root section
If (data! = null)
{
Console.WriteLine (data.Id)
Console.WriteLine (data.Time)
}
.net Framework configuration file 4. Write configuration file
Determine whether the configuration with the same name already exists before writing ConfigurationSectionGroup and ConfigurationSection, otherwise the write will fail.
In addition, if the configuration file is modified by other Configuration objects, the save fails and an exception is thrown. Singleton mode is recommended.
ExeConfigurationFileMap file =
New ExeConfigurationFileMap ()
File.ExeConfigFilename = "test.config"
Configuration config = ConfigurationManager.
OpenMappedExeConfiguration (file
ConfigurationUserLevel.None)
ConfigSectionData data =
New ConfigSectionData ()
Data.Id = 2000; data.Time = DateTime.Now
ConfigurationSectionGroup group1 =
Config.SectionGroups ["group1"]
If (group1 = = null) config.SectionGroups.
Add ("group1", new ConfigurationSectionGroup ())
ConfigurationSection data =
Group1.Sections ["add"] as config
If (add = = null) config.SectionGroups
["group1"] .Sections.add ("add", data)
Else {group1.Sections.Remove ("add")
Group1.Sections.Add ("add", data)
/ / or modify the original configuration object directly, provided that the type conversion is successful.
/ / ConfigSectionData configData =
Add as ConfigSectionData
/ / configData.Id = data.Id
/ / configData.Time = data.Time
}
Config.Save (ConfigurationSaveMode.Minimal)
.net Framework configuration file 5. Delete configuration section
Delete ConfigurationSectionGroup
Config.SectionGroups.
Remove ("group1")
/ / config.SectionGroups.Clear ()
Config.Save (Configuration
SaveMode.Minimal)
Delete ConfigurationSection
Config.Sections.Remove ("add1")
/ / config.Sections.Clear ()
If (config.SectionGroups ["group1"]
! = null)
{
Config.SectionGroups ["group1"].
Sections.Remove ("add2")
/ / config.SectionGroups ["group1"].
Sections.Clear ()
}
Config.Save (Configuration
SaveMode.Minimal)
.net Framework configuration file 6. Other
You can use ConfigurationManager.OpenMachineConfiguration () to manipulate the Machine.config file. Or use the WebConfigurationManager class in the System.Web.Configuration namespace to manipulate the ASP.net configuration file. ConfigurationManager also provides convenient operations such as AppSettings, ConnectionStrings, GetSection (), etc.
.net Framework configuration file 7. Use Custom CLA
For example, in ConfigSectionData, in addition to simple types, whether there can be custom classes. You can use custom classes, but you need to define a converter.
Using System
Using System.Collections; using System.
Collections.Generic
Using System.Configuration; using
System.Globalization
Using System.ComponentModel
/ / Custom class to write to the configuration file
Class CustomData
{
Public CustomData (string s)
{
This.s = s
}
Private string s
Public string S
{
Get
{
Return s
}
Set
{
S = value
}
}
}
/ / Custom converter (type judgment omitted in demo code)
Class CustomConvert: Configuration
ConverterBase
{
Public override bool CanConvertFrom
(ITypeDescriptorContext ctx, Type type)
{
Return (type = = typeof (string))
}
Public override object ConvertTo
(ITypeDescriptorContext ctx
CultureInfo ci, object value, Type type)
{
Return (value as CustomData)
}
Public override object ConvertFrom
(ITypeDescriptorContext ctx
CultureInfo ci, object data)
{
Return new CustomData ((string) data)
}
}
Class ConfigSectionData:
ConfigurationSection
{[ConfigurationProperty ("id")]
Public int Id
{
Get
{
Return (int) this ["id"]
}
Set
{
This ["id"] = value
}
}
[ConfigurationProperty ("time")]
Public DateTime Time {get
{
Return (DateTime) this ["time"]
}
Set
{
This ["time"] = value
}
}
[ConfigurationProperty ("custom")]
[TypeConverter (typeof (CustomConvert))]
/ / specify converter public CustomData Custom
{
Get
{
Return (CustomData) this ["custom"]
} set
{
This ["custom"] = value
}
}
}
Public class Program
{
Static void Main (string [] args)
{
Configuration config = ConfigurationManager.
OpenExeConfiguration (ConfigurationUserLevel.None)
ConfigSectionData data = new ConfigSectionData ()
Data.Id = 1000
Data.Time = DateTime.Now
Data.Custom = new CustomData ("abcdefg...")
Config.Sections.Add ("add", data)
Config.Save (ConfigurationSaveMode.Minimal)
/ / read test
ConfigSectionData configData =
(ConfigSectionData) config.Sections ["add"]
Bbs.bitsCN.com Console.WriteLine
(configData.Custom.S)
}
}
The above is the editor for you to share how to operate the .net Framework configuration file, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.