本文整理匯總了VB.NET中System.Configuration.ConfigurationPropertyAttribute類的典型用法代碼示例。如果您正苦於以下問題:VB.NET ConfigurationPropertyAttribute類的具體用法?VB.NET ConfigurationPropertyAttribute怎麽用?VB.NET ConfigurationPropertyAttribute使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了ConfigurationPropertyAttribute類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Name
' 導入命名空間
Imports System.Configuration
' Define a custom section.
' This class shows how to use the ConfigurationPropertyAttribute.
Public Class UrlsSection
Inherits ConfigurationSection
<ConfigurationProperty("name", DefaultValue:="Contoso", IsRequired:=True, IsKey:=True)>
Public Property Name() As String
Get
Return CStr(Me("name"))
End Get
Set(ByVal value As String)
Me("name") = value
End Set
End Property
<ConfigurationProperty("url", DefaultValue:="http://www.contoso.com", IsRequired:=True), RegexStringValidator("\w+:\/\/[\w.]+\S*")>
Public Property Url() As String
Get
Return CStr(Me("url"))
End Get
Set(ByVal value As String)
Me("url") = value
End Set
End Property
<ConfigurationProperty("port", DefaultValue:=0, IsRequired:=False), IntegerValidator(MinValue:=0, MaxValue:=8080, ExcludeRange:=False)>
Public Property Port() As Integer
Get
Return CInt(Fix(Me("port")))
End Get
Set(ByVal value As Integer)
Me("port") = value
End Set
End Property
End Class
示例2: UsingConfigurationPropertyAttribute
' 導入命名空間
Imports System.Configuration
Public Class UsingConfigurationPropertyAttribute
' Create a custom section and save it in the
' application configuration file.
Private Shared Sub CreateCustomSection()
Try
' Create a custom configuration section.
Dim customSection As New UrlsSection()
' Get the current configuration file.
Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
' Add the custom section to the application
' configuration file.
If config.Sections("CustomSection") Is Nothing Then
config.Sections.Add("CustomSection", customSection)
End If
' Save the application configuration file.
customSection.SectionInformation.ForceSave = True
config.Save(ConfigurationSaveMode.Modified)
Console.WriteLine("Created custom section in the application configuration file: {0}", config.FilePath)
Console.WriteLine()
Catch err As ConfigurationErrorsException
Console.WriteLine("CreateCustomSection: {0}", err.ToString())
End Try
End Sub
Private Shared Sub ReadCustomSection()
Try
' Get the application configuration file.
Dim config As System.Configuration.Configuration = TryCast(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None), Configuration)
' Read and display the custom section.
Dim customSection As UrlsSection = TryCast(config.GetSection("CustomSection"), UrlsSection)
Console.WriteLine("Reading custom section from the configuration file.")
Console.WriteLine("Section name: {0}", customSection.Name)
Console.WriteLine("Url: {0}", customSection.Url)
Console.WriteLine("Port: {0}", customSection.Port)
Console.WriteLine()
Catch err As ConfigurationErrorsException
Console.WriteLine("ReadCustomSection(string): {0}", err.ToString())
End Try
End Sub
Shared Sub Main(ByVal args() As String)
' Get the name of the application.
Dim appName As String = Environment.GetCommandLineArgs()(0)
' Create a custom section and save it in the
' application configuration file.
CreateCustomSection()
' Read the custom section saved in the
' application configuration file.
ReadCustomSection()
Console.WriteLine("Press enter to exit.")
Console.ReadLine()
End Sub
End Class