ConfigurationElement.LockItem 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定值,指出此項目是否已被鎖定。
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
,否則為 false
。 預設為 false
。
例外狀況
此項目已經在較高的組態層級上鎖定。
範例
下列範例示範如何使用 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
備註
LockItem如果您想要將一般鎖定放在專案本身及其子元素上,請使用 屬性。