Environment Variables can be used in any Operating System. Here we will focus on the Windows OS. These variables are just common shortcuts used to define something generic that can change between different machine. For example, the UserName of the current logged in user can differt from a computer to another. So instead of using the name of the user, we can use %username% to say: “put the name of the user here”. Which can be really usefull while coding (espacially for paths).
var path = "%username%";
var resolvedPath = Environment.ExpandEnvironmentVariables(path);
Code language: C# (cs)
Now we could go further and use more of these Environment Variables. Here is a list of the most common ones:
Environment Variable | Path |
%ALLUSERSPROFILE% | C:\ProgramData |
%APPDATA% | C:\Users\Username\AppData\Roaming |
%COMMONPROGRAMFILES% | C:\Program Files\Common Files |
%COMMONPROGRAMFILES(x86)% | C:\Program Files (x86)\Common Files |
%COMSPEC% | C:\Windows\System32\cmd.exe |
%HOMEDRIVE% | C: |
%HOMEPATH% | C:\Users\Username |
%LOCALAPPDATA% | C:\Users\Username\AppData\Local |
%PROGRAMDATA% | C:\ProgramData |
%PROGRAMFILES% | C:\Program Files |
%PROGRAMFILES(X86)% | C:\Program Files (x86) (only in 64-bit version) |
%PUBLIC% | C:\Users\Public |
%SystemDrive% | C: |
%SystemRoot% | C:\Windows |
%TEMP% and %TMP% | C:\Users\Username\AppData\Local\Temp |
%USERPROFILE% | C:\Users\Username |
%WINDIR% | C:\Windows |
More info about environment variables in this link.
Happy coding! 🙂