c sharp

C# Environment.UserDomainName Property

If you want to get your Network Domain name using C#, the UserDomainName property is used.

Environment Class in C# provides information about the working environment like variables, methods used, and system related information. It is possible to get the platform ID’s, Domain names, and check the modifiers present in C# application.Environment Class has vast range of applications in C# and .NET. It mainly tells us the system information without checking manually.

Environment.UserDomainName

UserDomainName Property in Environment class returns the Network Domain name.

Syntax:

string Environment.UserDomainName

 
Return Type:

String

Example 1:

Here, we will return the Network Domain name.

using System;
class Linuxhint
{
    //let's implement the UserDomainName property inside main method
    static public  void Main()
    {
        //get the network domain name
        Console.WriteLine("Domain: "+Environment.UserDomainName);
    }
}

 
Output:


We can see that the Network domain name of the user is “Chandu-PC”.

Explanation:

Line 5:

We are implementing our property inside the Main method.

Line 8:

Console.WriteLine("Domain: "+Environment.UserDomainName);

 
Here, we are directly using the UserDomainName property to return the name of the Network Domain.

Example 2:

Here, we will return the network domain name by storing it in a string variable.

Using System;
class Linuxhint
{
    //let’s implement the UserDomainName property inside main method
    static public  void Main()
    {
        //get the network domain name
        string my_net_name=Environment.UserDomainName;
        Console.WriteLine(“Domain: “+my_net_name);
    }
}

 
Output:

Explanation:

Line 8:

string my_net_name=Environment.UserDomainName;

 
Here, we are storing the name inside the string variable – my_net_name.

Line 9:

Console.WriteLine("Domain: "+my_net_name);

 
Finally, we are displaying the network domain name using the Console.WriteLine() function.

Conclusion

By the end of this article, we came to know that the UserDomainName property is used to return the name of the network domain that we are currently using. It returns the name in the form of a string. We discussed two examples of implementing this property in two ways.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain