tags:

views:

426

answers:

8

How do I get the computer name in .NET c#

+10  A: 

System.Environment.MachineName

Or, if you are using Winforms, you can use System.Windows.Forms.SystemInformation.ComputerName, which returns exactly the same value as System.Environment.MachineName.

Zach Johnson
+3  A: 

Environment.MachineName;

CesarGon
+6  A: 

System.Environment.MachineName

AlteredConcept
+2  A: 

tvanfosson says true and his answer is good but our work is done by one.. you not need more answer .. in programming we can do everything by various method. but we need only one method for solve our problem I always use System.Environment.MachineName for get the computer name in .NET

+26  A: 
tvanfosson
@tvanfosson: what is the difference ? I mean, which one should I use when? Does this have any security implications?
P.K
I would use the first from a console or winforms app, the second from a web app, and the third if I needed to get the FQDN. See the referenced documentation for information on the permissions required.
tvanfosson
A: 
string Name = System.Environment.MachineName;
Zach
+1  A: 

Well there is one more way: Windows Management Instrumentation

using System.Management;

try
        {
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("root\\CIMV2",
                "SELECT Name FROM Win32_ComputerSystem");

            foreach (ManagementObject queryObj in searcher.Get())
            {
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Win32_ComputerSystem instance");
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Name: {0}", queryObj["Name"]);
            }
        }
        catch (ManagementException e)
        {
            // exception handling
        }

MSDN

WMI

WMI Code creator

FAQs

PRR
+2  A: 

http://stackoverflow.com/questions/1072431/get-a-full-computer-name-or-domain-in-c

System.Environment.MachineName

Adam