Visual Basic Code Snippet - Access current environment directories and logical drives

  • Home
  • Code Snippets
  • VB
  • Visual Basic Code Snippet - Access current environment directories and logical drives

Visual Basic Code Snippet - Access current environment directories and logical drives

Visual Basic Code Snippet - Access current environment directories and logical drives

(VB) Visual Basic code snippets to access current environment directories such as current directory, system directory and logical drives.

Bookmark:

Visual Basic Code Snippet - Access current environment directories and logical drives

These .Net Visual Basic code snippets use Environment Class to access the current environment and platform to retrieve information about directories and logical drives. These code snippets shows how to use current environment to get current directory, system directory, temporary directory, home drive, application data directory, windows directory, system root, user profile directory and list all the available logical drives.

' Gets the fully qualified path of the current working directory.
Console.WriteLine("Current Directory: " & Environment.CurrentDirectory)

' Gets the fully qualified path of the system directory.
' Typical value - C:\WINDOWS\system32
Console.WriteLine("System Directory: " & Environment.SystemDirectory)

' Retrieves the contents of the specified variable from the environment 
' block of the calling process.
' Gets the temporary directory - %TEMP% or %TMP%
Console.WriteLine("GetEnvironment Variable: My temporary directory is " & Environment.GetEnvironmentVariable("TEMP"))

' Gets the home drive - %HOMEDRIVE%
' Typical value - C:
Console.WriteLine("Get Environment Variable: Home Drive " & Environment.GetEnvironmentVariable("HOMEDRIVE"))

' Gets Application Data directory - %APPDATA%
' Typical value - C:\Documents and Settings\{username}\Application Data
Console.WriteLine("Get Environment Variable: Application Data directory " & Environment.GetEnvironmentVariable("APPDATA"))

' Gets Windows directory - %WINDIR%
' Typical value - C:\WINDOWS
Console.WriteLine("Get Environment Variable: Windows directory " & Environment.GetEnvironmentVariable("WINDIR"))

' Gets Windows directory - %SYSTEMROOT%
' Typical value - The Windows XP root directory, usually C:\WINDOWS
Console.WriteLine("Get Environment Variable: System root " & Environment.GetEnvironmentVariable("SYSTEMROOT"))

' Gets User Profile directory - %USERPROFILE%
' Typical value - C:\Documents and Settings\{username}
Console.WriteLine("Get Environment Variable: User profile directory " & Environment.GetEnvironmentVariable("USERPROFILE"))


' Returns an array of string containing the names of the logical drives 
' on the current computer.
Dim drives() As String = Environment.GetLogicalDrives()
' Concatenates local drive list
Console.WriteLine("Get Logical Drives: " & String.Join(", ", drives))


VB Keywords Used:

  • Environment
  • Environment.CurrentDirectory
  • Environment.SystemDirectory
  • Environment.GetEnvironmentVariable
  • Environment.GetLogicalDrives

Code Snippet Information:

  • Applies To: Visual Studio, .Net, VB, Visual Basic, CLI, Environment Variables, Current Directory, System Directory, Temporary Directory, Application Data Directory, Windows Directory, System Root, User Profile Directory, Logical Drives
  • Programming Language : Visual Basic (VB)

External Resources:

Leave a comment