Export (0) Print
Expand All

InfiniteTimeSpanConverter Class

Converts between a string and the standard infinite TimeSpan value.

Namespace:  System.Configuration
Assembly:  System.Configuration (in System.Configuration.dll)

public sealed class InfiniteTimeSpanConverter : ConfigurationConverterBase

This type, like all the other configuration converter types, converts strings found in the configuration file to and from the related strongly typed properties.

In particular the InfiniteTimeSpanConverter converts the String value "infinite" assigned to a configuration property to the standard infinite TimeSpan and vice versa. The infinite TimeSpan value is represented by the MaxValue enumeration value.

This converter is used with TimeSpan properties. An infinite value is persisted as the "infinite" string.

The following code example shows how to define a custom section property that uses the InfiniteTimeSpanConverter class.

For the complete example code that implements the custom section, refer to the ConfigurationConverterBase class.

[ConfigurationProperty("timeDelay", 
    DefaultValue = "infinite")]
[TypeConverter(typeof(InfiniteTimeSpanConverter))]
public TimeSpan TimeDelay
{
    get
    {
        return (TimeSpan)this["timeDelay"];
    }
    set
    {
        this["timeDelay"] = value;
    }
}

The following example code shows how to access the previous custom section property.

using System;
using System.IO;
using System.ComponentModel;
using System.Configuration;

namespace Samples.AspNet
{

    public sealed class UsingInfiniteTimeSpanConverter
    {
        public static void GetTimeDelay()
        {
            try
            {
                CustomSection section =
                    ConfigurationManager.GetSection("CustomSection")
                    as CustomSection;

                Console.WriteLine("timeDelay: {0}", 
                    section.TimeDelay.ToString());
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public static void SetTimeDelay()
        {
            try
            {
                System.Configuration.Configuration config =
                  ConfigurationManager.OpenExeConfiguration(
                  ConfigurationUserLevel.None);

                CustomSection section =
                    config.Sections.Get("CustomSection")
                    as CustomSection;

                TimeSpan td = 
                    new TimeSpan();

                td =
                    TimeSpan.FromMinutes(
                    DateTime.Now.Minute);

                section.TimeDelay = td;

                section.SectionInformation.ForceSave = true;
                config.Save(ConfigurationSaveMode.Full); 
                config.Save();

                Console.WriteLine("timeDelay: {0}",
                    section.TimeDelay.ToString());
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

The following is a configuration excerpt as used by the previous example.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="CustomSection" 
      type="Samples.AspNet.CustomSection, 
      ConfigurationConverters, 
      Version=1.0.0.0, Culture=neutral, 
      PublicKeyToken=null" 
      allowDefinition="Everywhere" 
      allowExeDefinition="MachineToApplication" 
      restartOnExternalChanges="true" />
    </configSections>
    <CustomSection fileName="default.txt" maxIdleTime="90" timeDelay="infinite"/>
</configuration>

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft