Getting System Environment Values in .NET

  1. Sometimes it is necessary to get the system environment values such as mydocuments folder, Temp Directory, System Directory and so on. .NET Framework allows you to get these values.

  1. Add some labels and text boxes as shown below in the figure:

  1. Now we need to write some code for Button Click event as follows:

private void cmdGet_Click(object sender, System.EventArgs e)

{

txtMachineName.Text = System.Environment.MachineName;

txtOSVersion.Text = System.Environment.OSVersion.Platform.ToString() + " " + System.Environment.OSVersion.Version.Major.ToString();

txtUserDomain.Text = System.Environment.UserDomainName;

txtUserName.Text = System.Environment.UserName;

string[] strDrives = System.Environment.GetLogicalDrives();

string drives = null;

foreach(string str in strDrives )

{

drives = drives + str + " , ";

}

txtSystemDrives.Text = drives;

txtMyDocuments.Text = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

txtSystemFolder.Text = System.Environment.SystemDirectory;

txtTemporaryFolder.Text = System.Environment.GetEnvironmentVariable("TEMP");

txtProgramFiles.Text = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles);

}

  1. Run your program and test it out! You will get the important system environment values! That’s all for this Tutorial!
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s