傳回與作業系統環境變數關聯的字串。
命名空間: Microsoft.VisualBasic
組件: Microsoft.VisualBasic (在 microsoft.visualbasic.dll 中)
語法
'宣告
Public Shared Function Environ ( _
Expression As Integer _
) As String
'用途
Dim Expression As Integer
Dim returnValue As String
returnValue = Interaction.Environ(Expression)
public static string Environ (
int Expression
)
public:
static String^ Environ (
int Expression
)
public static String Environ (
int Expression
)
public static function Environ (
Expression : int
) : String
參數
- Expression
必要項。運算式,可評估包含環境變數名稱的字串,或評估與環境字串表中的環境字串數字順序相對應的整數。
傳回值
傳回與作業系統環境變數關聯的字串。
備註
如需詳細資訊,請參閱 Visual Basic 的主題 Environ 函式。
如果 Expression 包含字串,則 Environ 函式會傳回指派給指定之環境字串的文字,也就是環境字串表中該環境變數的等號 (=) 之後的文字。如果無法在環境字串表中找到 Expression 中的字串,則會傳回長度為零的字串 ("")。
如果 Expression 包含整數,則會傳回在環境字串表中佔據該數字位置的字串。在此情況下,Environ 會傳回所有文字,包括環境變數的名稱。如果指定之位置中沒有環境字串,則 Environ 會傳回長度為零的字串。
安全性注意事項 |
|---|
Environ 函式需要有環境使用權限,而這可能會影響它在部分信任狀況下的執行。如需詳細資訊,請參閱 SecurityPermission 和 程式碼存取使用權限。 |
範例
此範例會使用 Environ 函式,從環境字串表中提供項目編號及 PATH 陳述式的長度。
Sub tenv()
Dim envString As String
Dim found As Boolean = False
Dim index As Integer = 1
Dim pathLength As Integer
Dim message As String
envString = Environ(index)
While Not found And (envString <> "")
If (envString.Substring(0, 5) = "Path=") Then
found = True
Else
index += 1
envString = Environ(index)
End If
End While
If found Then
pathLength = Environ("PATH").Length
message = "PATH entry = " & index & " and length = " & pathLength
Else
message = "No PATH environment variable exists."
End If
MsgBox(message)
End Sub
平台
Windows 98、 Windows 2000 SP4、 Windows CE、 Windows Millennium Edition、 Windows Mobile for Pocket PC、 Windows Mobile for Smartphone、 Windows Server 2003、 Windows XP Media Center Edition、 Windows XP Professional x64 Edition、 Windows XP SP2、 Windows XP Starter Edition
.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。
版本資訊
.NET Framework
支援版本:2.0、1.1、1.0
請參閱
參考
Interaction 類別
Interaction 成員
Microsoft.VisualBasic 命名空間
ArgumentException
安全性注意事項