ApplicationBase.GetEnvironmentVariable(String) 方法

定义

返回指定环境变量的值。

public:
 System::String ^ GetEnvironmentVariable(System::String ^ name);
public string GetEnvironmentVariable (string name);
member this.GetEnvironmentVariable : string -> string
Public Function GetEnvironmentVariable (name As String) As String

参数

name
String

一个 String,包含环境变量名。

返回

String

一个 String,包含名为 name 的环境变量的值。

例外

nameNothing

name 指定的环境变量不存在。

调用代码没有访问权限为 ReadEnvironmentPermission

示例

此示例使用 My.Application.GetEnvironmentVariable 该方法获取并显示 PATH 环境变量的值(如果可用)。 否则,它将显示一条消息,指示 PATH 环境变量不存在。

Private Sub TestGetEnvironmentVariable()
    Try
        MsgBox("PATH = " & My.Application.GetEnvironmentVariable("PATH"))
    Catch ex As System.ArgumentException
        MsgBox("Environment variable 'PATH' does not exist.")
    End Try
End Sub

注解

该方法 My.Application.GetEnvironmentVariable 返回具有名称 name的环境变量。 此方法与 类似 Environment.GetEnvironmentVariable(String),除非此方法在不存在环境变量 name 的情况下引发异常。

可用性(按项目类型)

项目类型 可用
Windows 窗体应用程序
类库
控制台应用程序
Windows 窗体控件库
Web 控件库
Windows 服务
网站

适用于

另请参阅