System.Configuration.ConfigurationElement.IsModified 方法

方法描述

指示此配置元素自上次被保存或加载后,直到在派生类中被实现时,是否已经更改。

语法定义(C# System.Configuration.ConfigurationElement.IsModified 方法 的用法)

protected internal virtual bool IsModified()

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
返回值 System.Boolean 如果元素已修改,则为 true;否则为 false。

提示和注释

IsModified 方法可确定在当调用 Save 方法时,是否将此 ConfigurationElement 对象写入配置文件。 如果返回的值是 false,则假设该配置文件表示了元素的当前状态。

默认情况下,通过索引器为此 ConfigurationElement 对象设置了某个属性后,IsModified 将返回 true。

重写 IsModified 方法可提供此 ConfigurationElement 元素的状态的自定义指示。

System.Configuration.ConfigurationElement.IsModified 方法例子

修改配置元素后,将调用上一示例中演示的方法,如下例所示。

// 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());
            }

        }

异常

异常 异常描述

命名空间

namespace: System.Configuration

程序集: System.Configuration(在 System.Configuration.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。