My.Application.GetEnvironmentVariable 方法

更新:2007 年 11 月

返回指定环境变量的值。

' Usage
Dim value As String = My.Application.GetEnvironmentVariable(name)
' Declaration
Public Function GetEnvironmentVariable( _
   ByVal name As String _
) As String

参数

  • name
    包含环境变量名称的 String。

返回值

包含环境变量(名称为 name)的值的 String。

异常

下面的情况可能会导致异常:

备注

My.Application.GetEnvironmentVariable 方法返回名称为 name 的环境变量。此方法类似于 Environment.GetEnvironmentVariable(String),只是如果 name 指定的环境变量不存在,此方法将引发异常。

示例

此示例使用 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

要求

命名空间:Microsoft.VisualBasic.ApplicationServices

类:WindowsFormsApplicationBaseApplicationBase

**程序集:**Visual Basic 运行库(位于 Microsoft.VisualBasic.dll 中)

按项目类型列出的可用性

项目类型

可用

Windows 应用程序

类库

控制台应用程序

Windows 控件库

Web 控件库

Windows 服务

网站

权限

不需要任何权限。

请参见

参考

My.Application 对象

GetEnvironmentVariable

GetEnvironmentVariable