共用方式為


CodeDomProvider.GetLanguageFromExtension(String) 方法

定義

傳回與指定副檔名關聯的語言名稱,如 CodeDomProvider 編譯器組態區段的設定。

public:
 static System::String ^ GetLanguageFromExtension(System::String ^ extension);
public static string GetLanguageFromExtension (string extension);
[System.Runtime.InteropServices.ComVisible(false)]
public static string GetLanguageFromExtension (string extension);
static member GetLanguageFromExtension : string -> string
[<System.Runtime.InteropServices.ComVisible(false)>]
static member GetLanguageFromExtension : string -> string
Public Shared Function GetLanguageFromExtension (extension As String) As String

參數

extension
String

副檔名。

傳回

與指定副檔名關聯的語言名稱,如 CodeDomProvider 編譯器組態設定的設定。

屬性

例外狀況

extension 在這部電腦上沒有已設定的語言提供者。

extensionnull

呼叫端沒有必要的權限。

範例

下列程式代碼範例會 CodeDomProvider 決定輸入檔擴展名的實作,並顯示語言提供者的已設定設定。 此程式代碼範例是針對 類別提供的較大範例的 CompilerInfo 一部分。

if (  !fileExtension->StartsWith(  "." ) )
   fileExtension = String::Concat( ".", fileExtension );

// Get the language associated with the file extension.
CodeDomProvider^ provider = nullptr;
if ( CodeDomProvider::IsDefinedExtension( fileExtension ) )
{
   String^ language = CodeDomProvider::GetLanguageFromExtension( fileExtension );
   if ( language )
      Console::WriteLine( "The language \"{0}\" is associated with file extension \"{1}\"\n",
                           language, fileExtension );

   // Check for a corresponding language provider.
   if ( language && CodeDomProvider::IsDefinedLanguage( language ) )
   {
      provider = CodeDomProvider::CreateProvider( language );
      if ( provider )
      {
         // Display information about this language provider.
         Console::WriteLine( "Language provider:  {0}\n", provider->ToString() );
         
         // Get the compiler settings for this language.
         CompilerInfo^ langCompilerInfo = CodeDomProvider::GetCompilerInfo( language );
         if ( langCompilerInfo )
         {
            CompilerParameters^ langCompilerConfig = langCompilerInfo->CreateDefaultCompilerParameters();
            if ( langCompilerConfig )
            {
               Console::WriteLine( "  Compiler options:        {0}", langCompilerConfig->CompilerOptions );
               Console::WriteLine( "  Compiler warning level:  {0}", langCompilerConfig->WarningLevel.ToString() );
            }
         }
      }
   }
}

if ( provider == nullptr )  // Tell the user that the language provider was not found.
   Console::WriteLine( "There is no language provider associated with input file extension \"{0}\".", fileExtension );
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);
}
If Not fileExtension.StartsWith(".") Then
   fileExtension = "." + fileExtension
End If

' Get the language associated with the file extension.
If CodeDomProvider.IsDefinedExtension(fileExtension) Then
   Dim provider As CodeDomProvider
   Dim language As String = CodeDomProvider.GetLanguageFromExtension(fileExtension)
   
   Console.WriteLine("The language ""{0}"" is associated with file extension ""{1}""", _
       language, fileExtension)
   Console.WriteLine()
   
   ' Check for a corresponding language provider.
   If CodeDomProvider.IsDefinedLanguage(language) Then
      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.
      Dim langCompilerInfo As CompilerInfo = CodeDomProvider.GetCompilerInfo(language)
      Dim langCompilerConfig As CompilerParameters = langCompilerInfo.CreateDefaultCompilerParameters()
      
      Console.WriteLine("  Compiler options:        {0}", _
          langCompilerConfig.CompilerOptions)
      Console.WriteLine("  Compiler warning level:  {0}", _
          langCompilerConfig.WarningLevel)
   End If
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)
End If

備註

電腦組態檔中的 system.codedom> 元素 (Machine.config) 包含電腦上每個實作的語言提供者和編譯程式組態設定。<CodeDomProvider 方法會 GetLanguageFromExtension 搜尋每個提供者組態專案,以取得指定的擴展名。

方法 IsDefinedExtension 會檢查至少一個提供者實作是否支援特定的擴展名。 您可以將擴展名傳遞至 GetLanguageFromExtension之前,先使用 IsDefinedExtension 驗證擴展名。 這可防止 GetLanguageFromExtension 針對不支援的擴展名擲回 System.Configuration.ConfigurationException

如果提供者實作支援輸入檔擴展名,且該提供者已設定多個支持的語言,則 GetLanguageFromExtension 傳回該提供者的第一個語言名稱。 如果為輸入檔擴展名設定多個提供者實作,請 GetLanguageFromExtension 從最後一個相符的提供者組態專案傳回語言名稱。

語言名稱和擴展名不區分大小寫。

適用於

另請參閱