本文整理匯總了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...