CodeDomProvider.IsDefinedExtension(String) Method

Definition

Tests whether a file name extension has an associated CodeDomProvider implementation configured on the computer.

C#
public static bool IsDefinedExtension(string extension);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public static bool IsDefinedExtension(string extension);

Parameters

extension
String

A file name extension.

Returns

true if a CodeDomProvider implementation is configured for the specified file name extension; otherwise, false.

Attributes

Exceptions

The extension is null.

The caller does not have the required permission.

Examples

The following code example determines the CodeDomProvider implementation for an input file name extension and displays the configured settings for the language provider. This code example is part of a larger example provided for the CompilerInfo class.

C#
if (fileExtension[0] != '.')
{
    fileExtension = "." + fileExtension;
}

// Get the language associated with the file extension.
if (CodeDomProvider.IsDefinedExtension(fileExtension))
{
    CodeDomProvider provider;
    String language = CodeDomProvider.GetLanguageFromExtension(fileExtension);

    Console.WriteLine("The language \"{0}\" is associated with file extension \"{1}\"",
        language, fileExtension);
    Console.WriteLine();

    // Next, check for a corresponding language provider.

    if (CodeDomProvider.IsDefinedLanguage(language))
    {
        provider = CodeDomProvider.CreateProvider(language);

        // Display information about this language provider.

        Console.WriteLine("Language provider:  {0}",
            provider.ToString());
        Console.WriteLine();

        // Get the compiler settings for this language.

        CompilerInfo langCompilerInfo = CodeDomProvider.GetCompilerInfo(language);
        CompilerParameters langCompilerConfig = langCompilerInfo.CreateDefaultCompilerParameters();

        Console.WriteLine("  Compiler options:        {0}",
            langCompilerConfig.CompilerOptions);
        Console.WriteLine("  Compiler warning level:  {0}",
            langCompilerConfig.WarningLevel);
    }
}
else
{
    // Tell the user that the language provider was not found.
    Console.WriteLine("There is no language provider associated with input file extension \"{0}\".",
        fileExtension);
}

Remarks

The <system.codedom> Element in the machine configuration file (Machine.config) contains the language provider and compiler configuration settings for each CodeDomProvider implementation on the computer. The IsDefinedExtension method searches the provider configuration elements for the specified file name extension.

File name extensions are case-insensitive.

Applies to

Product Versions
.NET 8 (package-provided), 9 (package-provided)
.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 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also