Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, May 3, 2006 5:30 AM | 4 votes
Hi all,
Does anyone know how to get the computer name using c#?
Thanks
All replies (21)
Wednesday, May 3, 2006 6:39 AM ✅Answered | 38 votes
System.Environment.MachineName
Monday, August 13, 2007 9:09 AM
i cannot use the
"System.Environment.MachineName " .
some one told me to use=
using System.Security.Principal;
WindowsIdentity.GetCurrent().Name.ToString();
but the last line isn't working.
plz, i need this solution.
Monday, August 13, 2007 9:27 AM | 3 votes
Try using this
Code Snippet
SystemInformation.ComputerName
Wednesday, April 2, 2008 12:21 PM | 1 vote
Wednesday, April 2, 2008 12:24 PM | 1 vote
System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
Wednesday, July 1, 2009 4:43 PM
This works. no ned to import System.Security explicitly. // using System.Security.Principal;
My nephew says: "Mathematics and Politics are a part of life."
Thursday, September 3, 2009 8:09 PM | 2 votes
string serverName = System.Windows.Forms.SystemInformation.ComputerName;
Monday, June 7, 2010 12:24 PM | 7 votes
using System;
using System.Net;
public static string GetFQDN()
{
string domainName = NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
string hostName = Dns.GetHostName();
string fqdn = “”;
if (!hostName.Contains(domainName))
fqdn = hostName + “.” + domainName;
else
fqdn = hostName;
return fqdn;
}
Hope this helps
:)
Wednesday, June 9, 2010 3:38 PM
Hello Mattias...Thank you. it helped me.
Wednesday, June 9, 2010 4:06 PM | 1 vote
Why don't you just use
string machineName = System.Environment.MachineName;
Console.WriteLine(machineName);
Wednesday, December 21, 2011 12:58 PM
Yes. It works system.Environment.Machinename giving the machine name.
Thursday, December 22, 2011 11:16 PM
I'm new to C#. I played with sach's code just a bit and go this to work:
private void button1_Click(object sender, EventArgs e)
{
string domainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
string hostName = Dns.GetHostName();
string fqdn = "";
if (!hostName.Contains(domainName))
fqdn = hostName + "." + domainName;
else
fqdn = hostName;
MessageBox.Show(fqdn);
}
Monday, April 9, 2012 10:43 PM
Try this, string computer_name = System.Environment.GetEnvironmentVariable("COMPUTERNAME");
Naren.
Thursday, December 27, 2012 3:29 PM
Al final esta fue la que me funciono:
//Se imprimen los valores iniciales de la sesión
textBoxSistema.Text = System.Windows.Forms.SystemInformation.ComputerName;
Tuesday, April 30, 2013 8:40 PM
Do not use IP/DNS based solutions. They will fail in DHCP environments.
Wednesday, July 3, 2013 3:16 PM
Works fine.
Sunday, May 25, 2014 4:38 PM
Private m_CurUser As String
Public ReadOnly Property CurrentUser As String
Get
If String.IsNullOrEmpty(m_CurUser) Then
Dim who As System.Security.Principal.IIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()
If who Is Nothing Then
m_CurUser = Environment.UserDomainName & "\ & Environment.UserName
Else
m_CurUser = who.Name
End If
End If
Return m_CurUser
End Get
End Property
Wednesday, February 11, 2015 11:09 AM
if i'm not mistaking this would return the current user's name
Wednesday, February 11, 2015 1:29 PM
if using ASP.NET MVC; use
HttpServerUtility ss = new HttpServerUtility();
ss.MachineName;
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Saturday, February 14, 2015 8:55 PM
using System;
using System.IO;
using System.Speech.Synthesis;
using System.Windows.Forms;
using System.Net;
private void Button_Click(object sender, EventArgs e)
{
string domainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
string hostName = Dns.GetHostName();
string fqdn = "";
if (!hostName.Contains(domainName))
fqdn = hostName + "." + domainName;
else
fqdn = hostName;
//MessageBox.Show(fqdn); //<-- this is a Box
ambiance_Label2.Text = (fqdn); //<-- Create a Label. Change the label to your pc Name :)
}
}
INJOY <3
Monday, March 4, 2019 6:29 PM
One more way, which will return the "full computer name" as per the Computer Properties dialog:
System.Net.Dns.GetHostEntry("localhost").HostName