當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET Environment.GetEnvironmentVariables方法代碼示例

本文整理匯總了VB.NET中System.Environment.GetEnvironmentVariables方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Environment.GetEnvironmentVariables方法的具體用法?VB.NET Environment.GetEnvironmentVariables怎麽用?VB.NET Environment.GetEnvironmentVariables使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Environment的用法示例。


在下文中一共展示了Environment.GetEnvironmentVariables方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: Sample

' Sample for the Environment.GetEnvironmentVariables method
Imports System.Collections

Class Sample
   Public Shared Sub Main()
      Console.WriteLine("GetEnvironmentVariables: ")
      For Each de As DictionaryEntry In Environment.GetEnvironmentVariables()
         Console.WriteLine("  {0} = {1}", de.Key, de.Value)
      Next 
   End Sub 
End Class 
' Output from the example is not shown, since it is:
'    Lengthy.
'    Specific to the machine on which the example is run.
'    May reveal information that should remain secure.
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:15,代碼來源:Environment.GetEnvironmentVariables

示例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.GetEnvironmentVariables

輸出:

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.GetEnvironmentVariables方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。