Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to Get and Set values of environment variables using “Environment Class” in C# (.net 2.0).

Author: Zunnair
Download Source Code : 864_source.zip

In this article you will learn that how we can Get and Set environment variables using environment class in C# and VB (.net 20.).

 

Set Environment Variable:
Environment class provides us a facility to set the values of environment variables. For this purpose we can use Set Environment Variable () function. This function takes string variable and string value of environment. When this function invoke environment variable value get set.
To demonstrate make a window application. Drag two text boxes and one button on the form. Write environment name in first text box, write value in second text box and press button.
Now write the following code on button click event
C#
private void btn_set_Click(object sender, EventArgs e)
        {
            string str=string.Empty;           
            str = txt_name.Text;
            String str2 = string.Empty;
            str2=txt_value.Text;
            if (str != string.Empty && str2 != string.Empty)
            {
                Environment.SetEnvironmentVariable(str, str2);
                MessageBox.Show("VALUE SET");
            }
            else
                MessageBox.Show("INFORMATION MISSING");
        }
VB
Private Sub btn_set_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim str As String = String.Empty
        str = txt_name.Text
        Dim str2 As [String] = String.Empty
        str2 = txt_value.Text
        If str <> String.Empty AndAlso str2 <> String.Empty Then
            Environment.SetEnvironmentVariable(str, str2)
            MessageBox.Show("VALUE SET")
        Else
            MessageBox.Show("INFORMATION MISSING")
        End If
    End Sub
This simple code sets value of environment variable.
Get Environment Variable:
Environment class provides us a facility to get the environment variables value. For this purpose we can use Get Environment Variable () function. This function takes string variable name of environment. When this function invokes variable value get.
To demonstrate make a window application. Drag two text boxes and one button on the form. Write environment name in first text box, than press button. Value will come in second text box.
Now write the following code on button click event:
 
C#
private void btn_Get_Click(object sender, EventArgs e)
        {
            string STR;
            STR = txt_name2.Text;
            if (STR != string.Empty)
            {
                String STR2 = Environment.GetEnvironmentVariable(STR);
                txt_Value2.Text = STR2;
            }
            else
                MessageBox.Show("INFORMATION MISSING");
        }
VB
Private Sub btn_Get_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim STR As String
        STR = txt_name2.Text
        If STR <> String.Empty Then
            Dim STR2 As [String] = Environment.GetEnvironmentVariable(STR)
            txt_Value2.Text = STR2
        Else
            MessageBox.Show("INFORMATION MISSING")
        End If
    End Sub
This is simple code to get value of environment variable.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
 
            this.Text = "Devasp.net Application";
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
 
        Me.Text = "Devasp.net Application"
 End Sub
 
This simple article tells how we can to Get and Set environment variables using environment class in C# and VB (.net 20.).

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Char operations (Is high surrogate, is low surrogate, is letter and is letter or digit) using “Char Class” in C# (.net 2.0) PART2.

Disclaimer - Privacy
© 2002-2017 DevASP.net