by Edward
24 June 2010 20:21
The "Environment" class of the System namespace is handy for getting and setting various operating system related information. The "Environment" class to retrieve information such as the current directory, network details, machine name, OS versions, environment variable settings, contents of the call stack, time since last system boot, and the version of the common language runtime.
There is a lot of information that can be extracted with the "Environment" class. Below is a small code snippet that might help you get started.
public static void GetEnvironmentDetails()
{
Console.WriteLine();
Console.WriteLine("-- Environment members --");
// Invoke this sample with an arbitrary set of command line arguments.
Console.WriteLine("CommandLine: {0}", Environment.CommandLine);
String[] arguments = Environment.GetCommandLineArgs();
Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments));
// <-- Keep this information secure! -->
Console.WriteLine("CurrentDirectory: {0}", Environment.CurrentDirectory);
// <-- Keep this information secure! -->
Console.WriteLine("MachineName: {0}", Environment.MachineName);
Console.WriteLine("OSVersion: {0}", Environment.OSVersion);
Console.WriteLine("StackTrace: '{0}'", Environment.StackTrace);
// <-- Keep this information secure! -->
Console.WriteLine("SystemDirectory: {0}", Environment.SystemDirectory);
Console.WriteLine("TickCount: {0}", Environment.TickCount);
// <-- Keep this information secure! -->
Console.WriteLine("UserDomainName: {0}", Environment.UserDomainName);
Console.WriteLine("UserInteractive: {0}", Environment.UserInteractive);
// <-- Keep this information secure! -->
Console.WriteLine("UserName: {0}", Environment.UserName);
Console.WriteLine("Version: {0}", Environment.Version);
Console.WriteLine("GetFolderPath: {0}",
Environment.GetFolderPath(Environment.SpecialFolder.System));
String[] drives = Environment.GetLogicalDrives();
Console.WriteLine("GetLogicalDrives: {0}", String.Join(", ", drives));
}
by Edward
04 June 2010 06:51
The 'is' operator is used to check whether the run-time type of an object is compatible with a given type, and is a "reference type". This is very handy when you have code somewhere in a business or data layer and you cannot figure out why an exception is occuring.
For example:
public static void RunThis()
{
// For this example the following obj is a class already created
object obj = new MyClass();
CastCheck(obj);
}
public static void CastCheck(object obj)
{
// For this example the following classes are already created
Class1 a;
Class2 b;
if (obj is Class1)
{
Console.WriteLine("obj is Class1");
a = (Class1)obj; // do something with a
}
else if (obj is Class2)
{
Console.WriteLine("v is Class2");
b = (Class2)obj; // do something with b
}
else
{
Console.WriteLine("obj is neither Class1 nor Class2.");
}
}
The 'is' operator only considers reference conversions, boxing conversions, and unboxing conversions. Other conversions, such as user-defined conversions, are not considered by the is operator.
by Edward
01 June 2010 08:51
The 'as' operator is a "type", and is just like the cast operator except that it will return NULL on a conversion failure instead of throwing an exception. The 'as' operator is used to perform conversions between compatible types and is a "reference type". This is very handy when you have code somewhere in a business or data layer and you cannot figure out why an exception is occuring.
For example:
Object objValue = new Object();
string strA = (string)objValue; //Cast throws an Exception
string strB = objValue as string; //No exception is thrown, but strB is set to NULL
The 'as' operator only performs reference conversions and boxing conversions. The as operator cannot perform other conversions, such as user-defined conversions, which should instead be performed using cast expressions.