本文整理汇总了VB.NET中System.Environment.SetEnvironmentVariable方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Environment.SetEnvironmentVariable方法的具体用法?VB.NET Environment.SetEnvironmentVariable怎么用?VB.NET Environment.SetEnvironmentVariable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Environment 的用法示例。
在下文中一共展示了Environment.SetEnvironmentVariable方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。
示例1: 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
开发者ID:VB.NET开发者,项目名称:System,代码行数:41,代码来源:Environment.SetEnvironmentVariable 输出:
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.
示例2: 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
开发者ID:VB.NET开发者,项目名称:System,代码行数:80,代码来源:Environment.SetEnvironmentVariable 输出:
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...
注:本文中的System.Environment.SetEnvironmentVariable方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论