ConfigurationElement.LockItem Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets a value indicating whether the element is locked.
public:
property bool LockItem { bool get(); void set(bool value); };
public bool LockItem { get; set; }
member this.LockItem : bool with get, set
Public Property LockItem As Boolean
true
if the element is locked; otherwise, false
. The default is false
.
The element has already been locked at a higher configuration level.
The following example shows how to use the LockItem.
// Show how to set LockItem
// It adds a new UrlConfigElement to
// the collection.
static void LockItem()
{
string name = "Contoso";
string url = "http://www.contoso.com/";
int port = 8080;
try
{
// Get the current configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
// Get the MyUrls section.
UrlsSection myUrls =
config.Sections["MyUrls"] as UrlsSection;
// Create the new element.
UrlConfigElement newElement =
new UrlConfigElement(name, url, port);
// Set its lock.
newElement.LockItem = true;
// Save the new element to the
// configuration file.
if (!myUrls.ElementInformation.IsLocked)
{
myUrls.Urls.Add(newElement);
config.Save(ConfigurationSaveMode.Full);
// This is used to obsolete the cached
// section and read the updated
// bersion from the configuration file.
ConfigurationManager.RefreshSection("MyUrls");
}
else
Console.WriteLine(
"Section was locked, could not update.");
}
catch (ConfigurationErrorsException e)
{
Console.WriteLine("[LockItem: {0}]",
e.ToString());
}
}
' Show how to set LockItem
' It adds a new UrlConfigElement to
' the collection.
Shared Sub LockItem()
Dim name As String = "Contoso"
Dim url As String = "http://www.contoso.com/"
Dim port As Integer = 8080
Try
' Get the current configuration file.
Dim config _
As System.Configuration.Configuration = _
ConfigurationManager.OpenExeConfiguration( _
ConfigurationUserLevel.None)
' Get the MyUrls section.
Dim myUrls As UrlsSection = _
config.Sections("MyUrls")
' Create the new element.
Dim newElement _
As New UrlConfigElement(name, url, port)
' Set its lock.
newElement.LockItem = True
' Save the new element to the
' configuration file.
If Not myUrls.ElementInformation.IsLocked Then
myUrls.Urls.Add(newElement)
config.Save(ConfigurationSaveMode.Full)
' This is used to obsolete the cached
' section and read the updared version
' from the configuration file.
ConfigurationManager.RefreshSection("MyUrls")
Else
Console.WriteLine("Section was locked, could not update.")
End If
Catch e As ConfigurationErrorsException
Console.WriteLine("[LockItem: {0}]", _
e.ToString())
End Try
End Sub
Use the LockItem property if you want to put a general lock on the element itself and its child elements.
Product | Versies |
---|---|
.NET | 8 (package-provided), 9 (package-provided) |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1 |
.NET Standard | 2.0 (package-provided) |
Windows Desktop | 3.0, 3.1, 5, 6, 7, 8, 9 |
Feedback over .NET
.NET is een opensourceproject. Selecteer een koppeling om feedback te geven: