共用方式為


Environment.ExpandEnvironmentVariables(String) 方法

定義

用變數值的等值字串來取代內嵌於指定字串內的每個環境變數名稱,然後傳回結果字串。

public:
 static System::String ^ ExpandEnvironmentVariables(System::String ^ name);
public static string ExpandEnvironmentVariables (string name);
static member ExpandEnvironmentVariables : string -> string
Public Shared Function ExpandEnvironmentVariables (name As String) As String

參數

name
String

含有零或多個環境變數名稱的字串。 每個環境變數會以百分比符號字元 (%) 括住。

傳回

String

由字串值取代每個環境變數的字串。

例外狀況

namenull

範例

下列範例示範如何取得系統磁片磁碟機和系統根變數。

// Sample for the Environment::ExpandEnvironmentVariables method
using namespace System;
int main()
{
   String^ str;
   String^ nl = Environment::NewLine;
   Console::WriteLine();
   
   //  <-- Keep this information secure! -->
   String^ query = "My system drive is %SystemDrive% and my system root is %SystemRoot%";
   str = Environment::ExpandEnvironmentVariables( query );
   Console::WriteLine( "ExpandEnvironmentVariables: {0} {1}", nl, str );
}

/*
This example produces the following results:

ExpandEnvironmentVariables:
My system drive is C: and my system root is C:\WINNT
*/
// Sample for the Environment.ExpandEnvironmentVariables method
using System;

class Sample
{
    public static void Main()
    {
        // Keep this information secure!
        string query = "My system drive is %SystemDrive% and my system root is %SystemRoot%";

        string str = Environment.ExpandEnvironmentVariables(query);

        Console.WriteLine(str);
    }
}
/*
This example prints:

My system drive is C: and my system root is C:\WINDOWS
*/
// Sample for the Environment.ExpandEnvironmentVariables method
open System

let nl = Environment.NewLine

//  <-- Keep this information secure! -->
let query = "My system drive is %SystemDrive% and my system root is %SystemRoot%"
let str = Environment.ExpandEnvironmentVariables query
printfn $"\nExpandEnvironmentVariables: {nl}  {str}"

// This example produces the following results:
//     ExpandEnvironmentVariables:
//       My system drive is C: and my system root is C:\WINNT
' Sample for the Environment.ExpandEnvironmentVariables method
Class Sample
   Public Shared Sub Main()
      Dim str As [String]
      Dim nl As [String] = Environment.NewLine
      
      Console.WriteLine()
      '  <-- Keep this information secure! -->
      Dim query As [String] = "My system drive is %SystemDrive% and" & _ 
                              "my system root is %SystemRoot%"
      str = Environment.ExpandEnvironmentVariables(query)
      Console.WriteLine("ExpandEnvironmentVariables: {0}  {1}", nl, str)
   End Sub
End Class
'
'This example produces the following results:
'
'ExpandEnvironmentVariables:
'  My system drive is C: and my system root is C:\WINNT
'

備註

COM Interop 可用來從作業系統擷取環境變數。 如果因 COM 錯誤而無法擷取環境變數,則說明失敗原因的 HRESULT 是用來產生數個可能例外狀況之一;也就是說,例外狀況取決於 HRESULT。 如需如何處理 HRESULT 的詳細資訊,請參閱 方法的 Marshal.ThrowExceptionForHR 一節。

只會針對已設定的環境變數進行取代。 例如,假設 name 是 「MyENV = %MyENV%」。 如果環境變數 MyENV 設定為 42,這個方法會傳回 「MyENV = 42」。 如果未設定 MyENV,則不會發生變更;這個方法會傳回 「MyENV = %MyENV%」。

傳回值的大小限制為 32K。

適用於