GetLanguageFromExtension Method

CodeDomProvider.GetLanguageFromExtension Method

Returns a language name associated with the specified file name extension, as configured in the CodeDomProvider compiler configuration section.

Namespace: System.CodeDom.Compiler
Assembly: System (in system.dll)

public static string GetLanguageFromExtension (
	string extension
/** @attribute ComVisibleAttribute(false) */ 
public static String GetLanguageFromExtension (
	String extension
public static function GetLanguageFromExtension (
	extension : String
) : String
Not applicable.



A file name extension.

Return Value

A language name associated with the file name extension, as configured in the CodeDomProvider compiler configuration settings.

Exception typeCondition


The extension does not have a configured language provider on this computer.


The extension is a null reference (Nothing in Visual Basic).


The caller does not have the required permission.

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 GetLanguageFromExtension method searches each provider configuration element for the specified file name extension.

The IsDefinedExtension method checks whether at least one provider implementation supports a specific file name extension. You can validate a file name extension using IsDefinedExtension before passing it to GetLanguageFromExtension. This prevents GetLanguageFromExtension from throwing a System.Configuration.ConfigurationException for an unsupported file name extension.

If a provider implementation supports the input file name extension, and there are multiple supported languages configured for that provider, then GetLanguageFromExtension returns the first language name for that provider. If more than one provider implementation is configured for the input file name extension, GetLanguageFromExtension returns the language name from the last matching provider configuration element.

Language names and file name extensions are case-insensitive.

The following code example determines the CodeDomProvider implementation for an input file name extension and displays the configured settings for the language provider.

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);

    // 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}", 

        // Get the compiler settings for this language.

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

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

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

© 2015 Microsoft