ConfigurationElement.IsModified メソッド

定義

派生クラスに実装された場合、この構成要素が最後の保存または読み込み以降に変更されたかどうかを示します。

C#
protected virtual bool IsModified ();
C#
protected internal virtual bool IsModified ();

戻り値

要素が変更された場合は true、それ以外の場合は false

次の例は、 を拡張 IsModifiedする方法を示しています。

C#
protected override bool IsModified()
{
    bool ret = base.IsModified();
    // You can enter your custom processing code here.
    return ret;
}

前の例で示したメソッドは、次の例のように、構成要素が変更されたときに呼び出されます。

C#
// Show how to use IsModified.
// This method modifies the port property
// of the url element named Microsoft and
// saves the modification to the configuration
// file. This in turn will cause the overriden
// UrlConfigElement.IsModified() mathod to be called. 
static void ModifyElement()
{
    try
    {
        // Get the configuration file.
        System.Configuration.Configuration config =
            ConfigurationManager.OpenExeConfiguration(
            ConfigurationUserLevel.None);

        // Get the MyUrls section.
        UrlsSection myUrlsSection =
            config.GetSection("MyUrls") as UrlsSection;

        UrlsCollection elements = myUrlsSection.Urls;

        IEnumerator elemEnum =
            elements.GetEnumerator();

        int i = 0;
        while (elemEnum.MoveNext())
        {
            if (elements[i].Name == "Microsoft")
            {
                elements[i].Port = 1010;
                bool readOnly = elements[i].IsReadOnly();
                break;
            }
            i += 1;
        }

        if (!myUrlsSection.ElementInformation.IsLocked)
        {

            config.Save(ConfigurationSaveMode.Full);

            // This to obsolete the MyUrls cached 
            // section and read the updated version
            // from the configuration file.
            ConfigurationManager.RefreshSection("MyUrls");
        }
        else
            Console.WriteLine(
                "Section was locked, could not update.");
    }

    catch (ConfigurationErrorsException err)
    {
        Console.WriteLine("[ModifyElement: {0}]",
            err.ToString());
    }
}

注釈

メソッドはIsModified、メソッドの呼び出し時Saveに、このConfigurationElementオブジェクトを構成ファイルに書き込むかどうかを決定します。 戻り値が false の場合、構成ファイルが要素の現在の状態を表していると見なされます。

既定では、 は、IsModifiedインデクサーを使用してプロパティをこのConfigurationElementオブジェクトに設定した後に を返trueします。

メソッドをオーバーライドして、 IsModified この ConfigurationElement 要素の状態をカスタムで示します。

適用対象

製品 バージョン
.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, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9