Skip to main content
.NET Framework Class Library
ConfigurationElementIsModified Method

Indicates whether this configuration element has been modified since it was last saved or loaded, when implemented in a derived class.

Namespace:   System.Configuration
Assembly:  System.Configuration (in System.Configuration.dll)
Syntax
Protected Friend Overridable Function IsModified As [%$TOPIC/cc19b3s9_en-us_VS_110_1_0_0_0_0%]
protected internal virtual [%$TOPIC/cc19b3s9_en-us_VS_110_1_0_1_0_0%] IsModified()
protected public:
virtual [%$TOPIC/cc19b3s9_en-us_VS_110_1_0_2_0_0%] IsModified()
abstract IsModified : unit -> [%$TOPIC/cc19b3s9_en-us_VS_110_1_0_3_0_0%]  
override IsModified : unit -> [%$TOPIC/cc19b3s9_en-us_VS_110_1_0_3_0_1%]

Return Value

Type: SystemBoolean
true if the element has been modified; otherwise, false.
Remarks

The IsModified method determines whether this ConfigurationElement object will be written to the configuration file when the Save method is called. If the return value is false, it is assumed that the configuration file represents the current state of the element.

By default, IsModified returns true after a property is set through the indexer to this ConfigurationElement object.

Override the IsModified method to provide custom indication of the state of this ConfigurationElement element.

Examples

The following example shows how to extend IsModified.

Protected Overrides Function IsModified() As Boolean 
        Dim ret As Boolean = MyBase.IsModified()
        ' Enter your custom processing code here. 
        Return ret

    End Function 'IsModified
End Class 'UrlConfigElement 
protected override bool IsModified()
{
    bool ret = base.IsModified();
    // You can enter your custom processing code here. 
    return ret;
}

The method shown in the previous example is called when a configuration element is modified, as in the following example.

    ' 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.  
    Shared Sub ModifyElement()
        Try 
            ' Get the current configuration file. 
            Dim config _
            As System.Configuration.Configuration = _
            ConfigurationManager.OpenExeConfiguration( _
            ConfigurationUserLevel.None)

            ' Get the MyUrls section. 
            Dim myUrlsSection As UrlsSection = _
            config.GetSection("MyUrls")


            Dim elements As UrlsCollection = _
            myUrlsSection.Urls


            Dim elemEnum As IEnumerator = _
            elements.GetEnumerator()

            Dim i As Integer = 0
            While elemEnum.MoveNext()
                If elements(i).Name = "Microsoft" Then
                    elements(i).Port = 1010
                    Dim [readOnly] As Boolean = _
                    elements(i).IsReadOnly()
                    Exit While 
                End If
                i += 1
            End While 

            If Not myUrlsSection.ElementInformation.IsLocked Then

                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.")
            End If 

        Catch err As ConfigurationErrorsException
            Console.WriteLine("[ModifyElement: {0}]", _
            err.ToString())
        End Try 

    End Sub 'ModifyElement
// 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());
            }

        }
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.