当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET EnvironmentVariableTarget枚举代码示例

本文整理汇总了VB.NET中System.EnvironmentVariableTarget枚举的典型用法代码示例。如果您正苦于以下问题:VB.NET EnvironmentVariableTarget枚举的具体用法?VB.NET EnvironmentVariableTarget怎么用?VB.NET EnvironmentVariableTarget使用的例子?那么, 这里精选的枚举代码示例或许可以为您提供帮助。


在下文中一共展示了EnvironmentVariableTarget枚举的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: 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,代码来源:EnvironmentVariableTarget

输出:

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.EnvironmentVariableTarget枚举示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。