Il miglior modo per ricordarsi qualcosa è, tipicamente, scriverlo, facendone una sorta di microrelazione. E quale posto è migliore del proprio Blog per questo genere di attività ? (sarà l'età, ma la mia memoria inizia a fare acqua da tutte le parti...).
La classe (statica) in oggetto lavora a stretto contatto con il S.O., presentando una serie di proprietà (statiche) e metodi (statici) utilizzati per interrogare/impostare il contesto di esecuzione corrente.
L'interesse per questa classe è nato nel momento in cui ho "scoperto" il metodo ExpandEnvirnmentVariables, che accetta un parametro stringa, rimpiazzando il nome di ogni variabile d'ambiente uin essa contenuta con il valore corrispondente, restituendo una stringa "decodificata".
Approfondendo l'argomento ho "scoperto" altri metodi (e proprietà) particolarmente interessanti (nulla di trascendentale, per carità, ma comunque interessante), che possono sempre tornare utili per conoscere il contesto in cui viene eseguita la nostra applicazione.
Il codice che segue aiuta ad illustrare i membri della classe in oggetto :
static void Main(string[] args)
{
Console.WriteLine("Classe Environment");
Console.WriteLine("");
Console.WriteLine("Environment.UserName : {0}", Environment.UserName);
Console.WriteLine("Environment.UserDomainName : {0}", Environment.UserDomainName);
Console.WriteLine("Environment.UserInteractive : {0}", Environment.UserInteractive);
Console.WriteLine("Environment.MachineName : {0}", Environment.MachineName);
Console.WriteLine("Environment.OSVersion.Platform : {0}", Environment.OSVersion.Platform.ToString());
Console.WriteLine("Environment.OSVersion.ServicePack : {0}", Environment.OSVersion.ServicePack);
Console.WriteLine("Environment.OSVersion.Version : {0}", Environment.OSVersion.Version.ToString());
Console.WriteLine("Environment.OSVersion.VersionString : {0}", Environment.OSVersion.VersionString);
Console.WriteLine("Environment.Framework Version : {0}", Environment.Version.ToString());
Console.WriteLine("Environment.TickCount : {0}", Environment.TickCount);
Console.WriteLine("");
Console.WriteLine("Environment.CommandLine : {0}", Environment.CommandLine);
Console.WriteLine("Environment.CurrentDirectory : {0}", Environment.CurrentDirectory);
Console.WriteLine("Environment.HasShutdownStarted : {0}", Environment.HasShutdownStarted.ToString());
Console.WriteLine("Environment.ProcessorCount : {0}", Environment.ProcessorCount.ToString());
Console.WriteLine("Environment.SystemDirectory : {0}", Environment.SystemDirectory);
Console.WriteLine("Environment.WorkingSet : {0}", Environment.WorkingSet.ToString());
Console.WriteLine("");
Console.WriteLine("Environment Variables (Environment.GetEnvironmentVariables)");
IDictionary tDict = Environment.GetEnvironmentVariables();
foreach (DictionaryEntry de in tDict)
{
Console.WriteLine("{0} - {1}", de.Key, de.Value);
}
Console.WriteLine("");
Console.WriteLine("Expanded Environment Variables (Environment.ExpandEnvironmentVariables)");
Console.WriteLine("Directory Programmi (%programfiles%) : {0}", Environment.ExpandEnvironmentVariables("%programfiles%"));
Console.WriteLine("Directory System Root (%systemroot%) : {0}", Environment.ExpandEnvironmentVariables("%systemroot%"));
Console.WriteLine("");
Console.WriteLine("Logical Drives (Environment.GetLogicalDrives)");
string[] tLogicalDrives = Environment.GetLogicalDrives();
foreach (string s in tLogicalDrives)
{
Console.WriteLine(s);
}
Console.ReadKey();
}
Sulla mia macchina ha restituito il seguente output :
Classe Environment
Environment.UserName : f.grande
Environment.UserDomainName : POKER
Environment.UserInteractive : True
Environment.MachineName : NWV-FABIOG-01
Environment.OSVersion.Platform : Win32NT
Environment.OSVersion.ServicePack :
Environment.OSVersion.Version : 6.0.6000.0
Environment.OSVersion.VersionString : Microsoft Windows NT 6.0.6000.0
Environment.Framework Version : 2.0.50727.312
Environment.TickCount : 7571773
Environment.CommandLine : "C:\Works\DotNet Works\EnvironmentClass\EnvironmentClass\bin\D[...]"
Environment.CurrentDirectory : C:\Works\DotNet Works\EnvironmentClass\EnvironmentClass[...]
Environment.HasShutdownStarted : False
Environment.ProcessorCount : 2
Environment.SystemDirectory : C:\Windows\system32
Environment.WorkingSet : 17014784
Environment Variables (Environment.GetEnvironmentVariables)
Path - C:\oracle\product\10.2.0\db_1\bin;C:\Windows\sy[...]
TEMP - C:\Users\FEBB7~1.GRA\AppData\Local\Temp
USERDNSDOMAIN - POKER.IT
PATHEXT - .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
USERDOMAIN - POKER
PROCESSOR_ARCHITECTURE - x86
SystemDrive - C:
WecVersionForRosebud.1398 - 2
APPDATA - C:\Users\f.grande\AppData\Roaming
windir - C:\Windows
LOCALAPPDATA - C:\Users\f.grande\AppData\Local
__COMPAT_LAYER - RunAsAdmin RunAsAdmin
configsetroot - C:\Windows\ConfigSetRoot
TMP - C:\Users\FEBB7~1.GRA\AppData\Local\Temp
USERPROFILE - C:\Users\f.grande
ProgramFiles - C:\Program Files
FP_NO_HOST_CHECK - NO
HOMEPATH - \Users\f.grande
COMPUTERNAME - NWV-FABIOG-01
ProgramData - C:\ProgramData
PERL5LIB - C:\oracle\product\10.2.0\db_1\perl\5.8[...]
NUMBER_OF_PROCESSORS - 2
PROCESSOR_IDENTIFIER - x86 Family 6 Model 15 Stepping 6, GenuineIntel
DXROOT - C:\Program Files\Visual Studio 2005 SDK\2007.02\VisualStudioIntegration\Tools\Sandcastle\
SystemRoot - C:\Windows
ComSpec - C:\Windows\system32\cmd.exe
LOGONSERVER - \\SWINDC30
USERNAME - f.grande
CommonProgramFiles - C:\Program Files\Common Files
PROCESSOR_LEVEL - 6
PROCESSOR_REVISION - 0f06
VS80COMNTOOLS - C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\
lib - C:\Program Files\SQLXML 4.0\bin\
ALLUSERSPROFILE - C:\ProgramData
PUBLIC - C:\Users\Public
OS - Windows_NT
HOMEDRIVE - C:
Expanded Environment Variables (Environment.ExpandEnvironmentVariables)
Directory Programmi (%programfiles%) : C:\Program Files
Directory System Root (%systemroot%) : C:\Windows
Logical Drives (Environment.GetLogicalDrives)
C:\
F:\
Chissà, magari non servirà mai a nessuno... O magari si...
As usual, attendo commenti.