C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: Clean-up of your program's resources does not occur. Finally statements are ignored.
FinallySystem: This method is located in the System namespace. It is a static method that receives an int—this is used as the exit code.
Program: The finally block is not executed if the physical process is terminated directly (by Exit).
C# program that calls Environment.Exit with finally
using System;
class Program
{
static void Main()
{
try
{
//
// Calls the Environment.Exit method and returns a zero status code.
// ... The finally statement is never reached.
//
Environment.Exit(0);
}
finally
{
Console.WriteLine("Finally statement");
}
}
}
Info: With the Environment ExpandEnvironmentVariables method, we expand these encoded variables.
Note: This program loops over a string array of environment variable string literals (they begin and also end with percent characters).
Next: We call ExpandEnvironmentVariables on each string literal and write the substituted result.
C# program that expands environment variables
using System;
class Program
{
static void Main()
{
string[] variables =
{
"%WINDIR%",
"%HOMEDRIVE%",
"%USERNAME%",
"%COMPUTERNAME%"
};
foreach (string v in variables)
{
string result = Environment.ExpandEnvironmentVariables(v);
Console.WriteLine(result);
}
}
}
Output
C:\Windows
C:
Sam2
SAM-PC2
Next: This program shows how the args parameter is received in Main. It also shows how to use GetCommandLineArgs.
Note: The first element in the string array returned by GetCommandLineArgs is the program executable path.
ArrayC# program that uses Environment.GetCommandLineArgs
using System;
class Program
{
static void Main(string[] args)
{
// Args does not contain the program path.
Console.WriteLine(string.Join(",", args));
// GetCommandLineArgs contains the program path as the first element.
string[] array = Environment.GetCommandLineArgs();
Console.WriteLine(string.Join(",", array));
}
}
Output
(Argument: dot net Codex)
(Second output line truncated.)
dot,net,Codex
C:\Users\...\bin\Release\test.exe,dot,net,Codex
C# program that calls GetFolderPath
using System;
class Program
{
static void Main()
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Console.WriteLine(path);
}
}
Output
Varies depending on the operating environment.
C:\Users\Sam2\Desktop
Tip: Logical drives may not be separate physically, but to the Windows operating system, they are "logically" separate.
Tip 2: A partition on a hard disk is a logical drive. A USB disk is also a logical as well as physical drive.
C# program that shows GetLogicalDrives
using System;
class Program
{
static void Main()
{
string[] value = Environment.GetLogicalDrives();
Console.WriteLine(string.Join(",", value));
}
}
Output
C:\,D:\,H:\
Tip: Environment.ProcessorCount returns an int. It indicates how many cores all your computer's processors have.
Int, uintNote: This program shows that the dual-core, single processor computer I execute it on returns 2 for ProcessorCount.
C# program that accesses ProcessorCount
using System;
class Program
{
static void Main()
{
Console.WriteLine(Environment.ProcessorCount);
}
}
Output
2
Tip: In Visual Studio, we can type in Environment.SpecialFolder, press period and get a list of values.
Next: For our example, we will use MyDocuments. This is an ideal place for business files. We combine SpecialFolder with GetFolderPath.
Locations:
Desktop
DesktopDirectory
MyDocuments
MyMusic
MyPictures
Programs
StartMenu
Startup
C# program that uses Environment.SpecialFolder
public partial class ExampleForm : Form
{
// Initialize the controls on the form in the constructor here.
public ExampleForm()
{
InitializeComponent();
openFileDialog1.InitialDirectory =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}
}
C# program that uses Environment.GetFolderPath
private void TestMethod()
{
//
// Store the location of Pictures folder in a string.
//
Environment.SpecialFolder special = Environment.SpecialFolder.MyPictures;
string folderLocation = Environment.GetFolderPath(special);
}