Getting Environmental Values from .NET

 

Dim strStartupPath1 As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
Dim strStartupPath2 As String = System.Windows.Forms.Application.StartupPath()
 
Dim strExecutablePath As String = System.Windows.Forms.Application.ExecutablePath()

Recently I wrote an ‘updater’ app that other apps call to check for a new version, the whole thing runs off of a web service. At any rate, I needed the calling app to send it’s machine name and the current application name. Never needed that before…

            Dim assem As Assembly = Assembly.GetEntryAssembly()
            Dim assemName As AssemblyName = assem.GetName()
            Dim ver As Version = assemName.Version

Here’s another way to get the machine name

Environment.MachineName.ToString

I also needed to know where the user’s applicationData folder was…

 

strApplicationData = _
(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))
strDataFile = Path.Combine(strApplicationData, "Importer\ImporterData.xml")

The System.Reflection.Assembly class

Represents an assembly, which is a reusable, versionable, and self-describing building block of a common language runtime application

Use it like this:

            Dim assem As Assembly = Assembly.GetEntryAssembly()
            Dim assemName As AssemblyName = assem.GetName()
            Dim ver As Version = assemName.Version

Other Useful Properties:

FullName - Gets the display name of the assembly

The System.Environment class

Provides information about, and means to manipulate, the current environment and platform. This class cannot be inherited.

Useful (but not all) properties:

CurrentDirectory
MachineName
NewLine - Get the newline string defined for this environment
OSVersion
SystemDirectory
UserDomainName
UserName
Version

Here are the values for the Enviroment.SpecialFolder enumeration:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Environment.SpecialFolder.CommonApplicationData() Environment.SpecialFolder.CommonProgramFiles() Environment.SpecialFolder.Cookies(Environment.SpecialFolder.Desktop) Environment.SpecialFolder.DesktopDirectory() Environment.SpecialFolder.Favorites(Environment.SpecialFolder.History) Environment.SpecialFolder.InternetCache(Environment.SpecialFolder.LocalApplicationData) Environment.SpecialFolder.MyComputer() Environment.SpecialFolder.MyMusic(Environment.SpecialFolder.MyPictures) Environment.SpecialFolder.Personal() Environment.SpecialFolder.ProgramFiles() Environment.SpecialFolder.Programs(Environment.SpecialFolder.Recent) Environment.SpecialFolder.SendTo() Environment.SpecialFolder.StartMenu()
Environment.Exit(0)

 

 

and methods:

GetCommandLineArgs
GetFolderPath (see above)

The Microsoft.VisualBasic.Environ Function

Returns the string associated with an operating-system environment variable

Use it like this:

Dim strUser as String
strUser = Environ("username")

Possible values:

ALLUSERSPROFILE
APPDATA
AVENGINE
CLIENTNAME
CommonProgramFiles
COMPUTERNAME
ComSpec
FP_NO_HOST_CHECK
HOMEDRIVE
HOMEPATH
INCLUDE
INOCULAN
LIB
LOGONSERVER
NUMBER_OF_PROCESSORS
OS
Path
PATHEXT
PROCESSOR_ARCHITECTURE
PROCESSOR_IDENTIFIER
PROCESSOR_LEVEL
PROCESSOR_REVISION
ProgramFiles
SESSIONNAME
SystemDrive
SystemRoot
TEMP
TMP
USERDOMAIN
USERNAME
USERPROFILE
VS71COMNTOOLS
WecVersionForRosebud.FF0
windir

The System.IO.Path class

We've written on the Path class before, but that time our interest was in the 'pathing' methods. This time we're looking at the environmental aspects:

Path.GetTempPath - Returns the path of the current user's temporary folder


 

RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences