Friday, May 21, 2021

C# Environmment static class

Environment is static class which provides information about, and means to manipulate, the current environment and platform.
Example: List all the enumerated constants for special folders

using System;
namespace ConsoleEnvironment
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (var item in Enum.GetNames(typeof(Environment.SpecialFolder)))
            {
                var enumValue = (int)Enum.Parse(typeof(Environment.SpecialFolder), item);
                Console.WriteLine(item + " " + enumValue);
            }
            Console.ReadKey();
        }
    }
}
OUTPUT
  • Desktop 0
  • Programs 2
  • MyDocuments 5
  • Personal 5
  • Favorites 6
  • Startup 7
  • Recent 8
  • SendTo 9
  • StartMenu 11
  • MyMusic 13
  • MyVideos 14
  • DesktopDirectory 16
  • MyComputer 17
  • NetworkShortcuts 19
  • Fonts 20
  • Templates 21
  • CommonStartMenu 22
  • CommonPrograms 23
  • CommonStartup 24
  • CommonDesktopDirectory 25
  • ApplicationData 26
  • PrinterShortcuts 27
  • LocalApplicationData 28
  • InternetCache 32
  • Cookies 33
  • History 34
  • CommonApplicationData 35
  • Windows 36
  • System 37
  • ProgramFiles 38
  • MyPictures 39
  • UserProfile 40
  • SystemX86 41
  • ProgramFilesX86 42
  • CommonProgramFiles 43
  • CommonProgramFilesX86 44
  • CommonTemplates 45
  • CommonDocuments 46
  • CommonAdminTools 47
  • AdminTools 48
  • CommonMusic 53
  • CommonPictures 54
  • CommonVideos 55
  • Resources 56
  • LocalizedResources 57
  • CommonOemLinks 58
  • CDBurning 59
Example: Alphabetical list of all the enumerated constants for special folders

using System;
namespace ConsoleEnvironment
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] MyArr = Enum.GetNames(typeof(Environment.SpecialFolder));
            Array.Sort(MyArr);
            foreach (var item in MyArr)
            {
                var enumValue = (int)Enum.Parse(typeof(Environment.SpecialFolder), item);
                Console.WriteLine(item + " " + enumValue);
            }
            Console.ReadKey();
        }
    }
}
OUTPUT
  • AdminTools 48
  • ApplicationData 26
  • CDBurning 59
  • CommonAdminTools 47
  • CommonApplicationData 35
  • CommonDesktopDirectory 25
  • CommonDocuments 46
  • CommonMusic 53
  • CommonOemLinks 58
  • CommonPictures 54
  • CommonProgramFiles 43
  • CommonProgramFilesX86 44
  • CommonPrograms 23
  • CommonStartMenu 22
  • CommonStartup 24
  • CommonTemplates 45
  • CommonVideos 55
  • Cookies 33
  • Desktop 0
  • DesktopDirectory 16
  • Favorites 6
  • Fonts 20
  • History 34
  • InternetCache 32
  • LocalApplicationData 28
  • LocalizedResources 57
  • MyComputer 17
  • MyDocuments 5
  • MyMusic 13
  • MyPictures 39
  • MyVideos 14
  • NetworkShortcuts 19
  • Personal 5
  • PrinterShortcuts 27
  • ProgramFiles 38
  • ProgramFilesX86 42
  • Programs 2
  • Recent 8
  • Resources 56
  • SendTo 9
  • StartMenu 11
  • Startup 7
  • System 37
  • SystemX86 41
  • Templates 21
  • UserProfile 40
  • Windows 36
Environment Other Examples

using System;
namespace ConsoleEnvironment
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
            Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
            Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
            Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic));
            Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.MyVideos));
            //Console.WriteLine(Environment.GetEnvironmentVariable("PATH"));
            Console.WriteLine("AppData Path: {0}",Environment.GetEnvironmentVariable("APPDATA"));
            Console.WriteLine("OS: {0}",Environment.GetEnvironmentVariable("OS"));
            Console.WriteLine("Username: {0}",Environment.GetEnvironmentVariable("USERNAME"));
            Console.WriteLine("Current Directory: {0}", Environment.CurrentDirectory);
            Console.WriteLine("Is64BitOperatingSystem: {0}", Environment.Is64BitOperatingSystem);
            Console.WriteLine("MachineName: {0}", Environment.MachineName);
            Console.WriteLine("OS Version: {0}",Environment.OSVersion);

            Console.ReadKey();
        }
    }
}
OUTPUT
  • C:\Users\AJEET\AppData\Local\Microsoft\Windows\INetCookies
  • C:\Users\AJEET\Desktop
  • C:\Users\AJEET\Documents
  • C:\Users\AJEET\Music
  • C:\Users\AJEET\Videos
  • AppData Path: C:\Users\AJEET\AppData\Roaming
  • OS: Windows_NT
  • Username: AJEET
  • Current Directory: C:\Users\AJEET\Desktop\Apple\MvcEx\ConsoleEnvironment\ConsoleEnvironment\bin\Debug
  • Is64BitOperatingSystem: True
  • MachineName: IN-AJEET-LT
  • OS Version: Microsoft Windows NT 6.2.9200.0
Example : To list all the Environment Variables

using System;
namespace ConsoleEnvironment
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Collections.IDictionary envsCollection = Environment.GetEnvironmentVariables();
            
            foreach (var envKey in envsCollection.Keys) //Also, envsCollection.Values 
            {
                Console.WriteLine(envKey + " -> "+Environment.GetEnvironmentVariable(envKey.ToString()));
            }
            Console.ReadKey();
        }
    }
}
OUTPUT
  • PROCESSOR_ARCHITEW6432 -> AMD64
  • DriverData -> C:\Windows\System32\Drivers\DriverData
  • COMPUTERNAME -> IN-AJEET-LT
  • CommonProgramFiles(x86) -> C:\Program Files (x86)\Common Files
  • OneDrive -> C:\Users\AJEET\OneDrive
  • HOMEPATH -> \Users\AJEET
  • VisualStudioEdition -> Microsoft Visual Studio Community 2019
  • ServiceHubLogSessionKey -> 06E4FFF5
  • CLASSPATH -> C:\Users\AJEET\Desktop\Java_Work
  • PROCESSOR_REVISION -> 8e09
  • PATHEXT -> .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
  • PkgDefApplicationConfigFile -> C:\Users\AJEET\AppData\Local\Microsoft\VisualStudio\16.0_3d68e095\devenv.exe.config
  • VS120COMNTOOLS -> C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\
  • TMP -> C:\Users\AJEET\AppData\Local\Temp
  • TEMP -> C:\Users\AJEET\AppData\Local\Temp
  • LOCALAPPDATA -> C:\Users\AJEET\AppData\Local
  • PUBLIC -> C:\Users\Public
  • VSAPPIDNAME -> devenv.exe
  • SignInWithHomeTenantOnly -> True
  • ProgramW6432 -> C:\Program Files
  • USERDOMAIN -> IN-AJEET-LT
  • ProgramFiles(x86) -> C:\Program Files (x86)
  • PROCESSOR_LEVEL -> 6
  • PROCESSOR_IDENTIFIER -> Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
  • ForceIdentityAuthenticationType -> Embedded
  • MSBuildLoadMicrosoftTargetsReadOnly -> true
  • _NO_DEBUG_HEAP -> 1
  • PSModulePath -> C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules;c:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\PowerShell\Modules\
  • NUMBER_OF_PROCESSORS -> 4
  • git -> C:\Program Files\Git\bin
  • CommonProgramFiles -> C:\Program Files (x86)\Common Files
  • NVTOOLSEXT_PATH -> C:\Program Files\NVIDIA Corporation\NvToolsExt\
  • VisualStudioDir -> C:\Users\AJEET\Documents\Visual Studio 2019
  • VSLS_SESSION_KEEPALIVE_INTERVAL -> 0
  • ProgramData -> C:\ProgramData
  • ProgramFiles -> C:\Program Files (x86)
  • PATH -> C:\Program Files\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;c:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;c:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;c:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Users\AJEET\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\150\DTS\Binn\;C:\Program Files\Azure Data Studio\bin;C:\Users\AJEET\AppData\Local\Microsoft\WindowsApps;C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.2\bin;;C:\Program Files\Java\jdk-11.0.8\bin;;C:\Users\AJEET\AppData\Local\Programs\Microsoft VS Code\bin;C:\Program Files\Azure Data Studio\bin;C:\Users\AJEET\.dotnet\tools
  • ChocolateyInstall -> C:\ProgramData\chocolatey
  • ENABLE_XAML_DIAGNOSTICS_SOURCE_INFO -> 1
  • PyCharm Community Edition -> C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.2\bin;
  • VSSKUEDITION -> Community
  • ThreadedWaitDialogDpiContext -> -4
  • SystemRoot -> C:\WINDOWS
  • VSAPPIDDIR -> C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\
  • SESSIONNAME -> Console
  • VisualStudioVersion -> 16.0
  • CommonProgramW6432 -> C:\Program Files\Common Files
  • LOGONSERVER -> \\IN-AJEET-LT
  • USERPROFILE -> C:\Users\AJEET
  • FPS_BROWSER_USER_PROFILE_STRING -> Default
  • VS140COMNTOOLS -> C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\
  • VSLANG -> 1033
  • USERDOMAIN_ROAMINGPROFILE -> IN-AJEET-LT
  • APPDATA -> C:\Users\AJEET\AppData\Roaming
  • HOMEDRIVE -> C:
  • configsetroot -> C:\WINDOWS\ConfigSetRoot
  • VS110COMNTOOLS -> C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\
  • USERNAME -> AJEET
  • FPS_BROWSER_APP_PROFILE_STRING -> Internet Explorer
  • PROCESSOR_ARCHITECTURE -> x86
  • OS -> Windows_NT
  • ComSpec -> C:\WINDOWS\system32\cmd.exe
  • SystemDrive -> C:
  • windir -> C:\WINDOWS
  • OneDriveConsumer -> C:\Users\AJEET\OneDrive
  • ALLUSERSPROFILE -> C:\ProgramData

No comments:

Post a Comment

Hot Topics