本文整理汇总了VB.NET中System.Environment.GetEnvironmentVariable方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Environment.GetEnvironmentVariable方法的具体用法?VB.NET Environment.GetEnvironmentVariable怎么用?VB.NET Environment.GetEnvironmentVariable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Environment
的用法示例。
在下文中一共展示了Environment.GetEnvironmentVariable方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Example
' 导入命名空间
Imports System.IO
Module Example
Public Sub Main()
If Environment.OSVersion.Platform = PlatformID.Win32NT Then
' Change the directory to %WINDIR%
Environment.CurrentDirectory = Environment.GetEnvironmentVariable("windir")
Dim info As New DirectoryInfo(".")
Console.WriteLine("Directory Info: " + info.FullName)
Else
Console.WriteLine("This example runs on Windows only.")
End If
End Sub
End Module
' The example displays output like the following on a .NET implementation running on Windows:
' Directory Info: C:\windows
' The example displays the following output on a .NET implementation on Unix-based systems:
' This example runs on Windows only.
示例2: Example
Module Example
Public Sub Main()
Dim value As String
Dim toDelete As Boolean = False
' Check whether the environment variable exists.
value = Environment.GetEnvironmentVariable("Test1")
' If necessary, create it.
If value Is Nothing Then
Environment.SetEnvironmentVariable("Test1", "Value1")
toDelete = True
' Now retrieve it.
value = Environment.GetEnvironmentVariable("Test1")
End If
' Display the value.
Console.WriteLine($"Test1: {value}")
Console.WriteLine()
' Confirm that the value can only be retrieved from the process
' environment block if running on a Windows system.
If Environment.OSVersion.Platform = PlatformID.Win32NT Then
Console.WriteLine("Attempting to retrieve Test1 from:")
For Each enumValue As EnvironmentVariableTarget In
[Enum].GetValues(GetType(EnvironmentVariableTarget))
value = Environment.GetEnvironmentVariable("Test1", enumValue)
Console.WriteLine($" {enumValue}: {If(value IsNot Nothing, "found", "not found")}")
Next
Console.WriteLine()
End If
' If we've created it, now delete it.
If toDelete Then
Environment.SetEnvironmentVariable("Test1", Nothing)
' Confirm the deletion.
If Environment.GetEnvironmentVariable("Test1") = Nothing Then
Console.WriteLine("Test1 has been deleted.")
End If
End If
End Sub
End Module
输出:
Test1: Value1 Attempting to retrieve Test1 from: Process: found User: not found Machine: not found Test1 has been deleted. The example displays the following output if run on a Unix-based system: Test1: Value1 Test1 has been deleted.
示例3: Main
' 导入命名空间
Imports System.Collections
Imports Microsoft.Win32
Module Sample
Public Sub Main()
' Environment variable names for default, process, user, and machine targets.
Dim defaultEnvVar As String = NameOf(defaultEnvVar)
Dim processEnvVar As String = NameOf(processEnvVar)
Dim userEnvVar As String = NameOf(userEnvVar)
Dim machineEnvVar As String = NameOf(machineEnvVar)
Dim dft As String = NameOf(dft)
Dim process As String = NameOf(process)
Dim user As String = NameOf(user)
Dim machine As String = NameOf(machine)
' Set the environment variable for each target.
Console.WriteLine("Setting environment variables for each target...")
' The default target (the current process).
Environment.SetEnvironmentVariable(defaultEnvVar, dft)
' The current process.
Environment.SetEnvironmentVariable(processEnvVar, process,
EnvironmentVariableTarget.Process)
' The current user.
Environment.SetEnvironmentVariable(userEnvVar, user,
EnvironmentVariableTarget.User)
' The local machine.
Environment.SetEnvironmentVariable(machineEnvVar, machine,
EnvironmentVariableTarget.Machine)
Console.WriteLine()
' Define an array of environment variables.
Dim envVars As String() = { defaultEnvVar, processEnvVar, userEnvVar, machineEnvVar }
' Try to get the environment variables from each target.
' The default (no specified target).
Console.WriteLine("Retrieving environment variables from the default target:")
For Each envVar in envVars
Dim value = Environment.GetEnvironmentVariable(envVar)
Console.WriteLine($" {envVar}: {If(value IsNot Nothing, value, "(none)")}")
Next
Console.WriteLine()
' The process block.
Console.WriteLine("Retrieving environment variables from the Process target:")
For Each envVar in envVars
Dim value = Environment.GetEnvironmentVariable(envVar, EnvironmentVariableTarget.Process)
Console.WriteLine($" {envVar}: {If(value IsNot Nothing, value, "(none)")}")
Next
Console.WriteLine()
' The user block.
Console.WriteLine("Retrieving environment variables from the User target:")
For Each envVar in envVars
Dim value = Environment.GetEnvironmentVariable(envVar, EnvironmentVariableTarget.User)
Console.WriteLine($" {envVar}: {value}")
Next
Console.WriteLine()
' The machine block.
Console.WriteLine("Retrieving environment variables from the Machine target:")
For Each envVar in envVars
Dim value = Environment.GetEnvironmentVariable(envVar, EnvironmentVariableTarget.Machine)
Console.WriteLine($" {envVar}: {value}")
Next
Console.WriteLine()
' Delete the environment variable for each target.
Console.WriteLine("Deleting environment variables for each target...")
' The default target (the current process).
Environment.SetEnvironmentVariable(defaultEnvVar, Nothing)
' The current process.
Environment.SetEnvironmentVariable(processEnvVar, Nothing,
EnvironmentVariableTarget.Process)
' The current user.
Environment.SetEnvironmentVariable(userEnvVar, Nothing,
EnvironmentVariableTarget.User)
' The local machine.
Environment.SetEnvironmentVariable(machineEnvVar, Nothing,
EnvironmentVariableTarget.Machine)
End Sub
End Module
输出:
Setting environment variables for each target... Retrieving environment variables from the default target: defaultEnvVar: dft processEnvVar: process userEnvVar: user machineEnvVar: (none) Retrieving environment variables from the Process target: defaultEnvVar: dft processEnvVar: process userEnvVar: user machineEnvVar: (none) Retrieving environment variables from the User target: defaultEnvVar: (none) processEnvVar: (none) userEnvVar: user machineEnvVar: (none) Retrieving environment variables from the Machine target: defaultEnvVar: (none) processEnvVar: (none) userEnvVar: (none) machineEnvVar: machine Deleting environment variables for each target... The example displays the following output if run on a Unix-based system: Setting environment variables for each target... Retrieving environment variables from the default target: defaultEnvVar: dft processEnvVar: process userEnvVar: (none) machineEnvVar: (none) Retrieving environment variables from the Process target: defaultEnvVar: dft processEnvVar: process userEnvVar: (none) machineEnvVar: (none) Retrieving environment variables from the User target: defaultEnvVar: (none) processEnvVar: (none) userEnvVar: (none) machineEnvVar: (none) Retrieving environment variables from the Machine target: defaultEnvVar: (none) processEnvVar: (none) userEnvVar: (none) machineEnvVar: (none) Deleting environment variables for each target...