28

I want to get the current computer name.

this is my code:

Public Function GetComputerName() As String
    Dim ComputerName As String
    ComputerName = System.Net.Dns.GetHostName
    Return ComputerName
End Function

this code working, but i remember that there is faster way.

what is the fast way to get the computer name?

Nh123
  • 997
  • 4
  • 10
  • 13
  • Just use this line of Code `System.Net.Dns.GetHostName` and remove the function or this `System.Windows.Forms.SystemInformation.ComputerName` – Kym NT Apr 28 '16 at 01:53

5 Answers5

60

you can just use without function:

System.Net.Dns.GetHostName

or:

Environment.MachineName
famf
  • 2,225
  • 19
  • 17
7

This is not that good the previous anwser,but if you love to work with forms:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    Me.Text = System.Windows.Forms.SystemInformation.ComputerName

End Sub

System.Windows.Forms.SystemInformation gives a lot of nice informations.

-> UserDomainName

-> UserName

Check-Kay Wong
  • 221
  • 1
  • 3
5

There is a nice static property that should work anywhere in your program and in both VB.NET and C#:

System.Environment.MachineName

Because System namespace is imported automatically by default, it should be enough to refer to it simply by typing Environment.MachineName.

For unknown reason My.Computer.Name is not working for many people (including me) despite it is often being mentioned as a correct way to get the current hostname. So you don't need to bother trying to make it work.

lot
  • 1,434
  • 18
  • 23
  • I do think by now you should have know what is the reason why you cant access "MY" in C#, but if you have not, then let me tell you this. C# does not supper "MY" Namespace. – Mr.J Jul 05 '18 at 03:38
3

An other way to get the computer name not mentioned in the previous responses:

My.Computer.Name

Edit

Works for VB.NET only, not C#

Rémi Gaudin
  • 414
  • 10
  • 14
  • I don't know why, but despite this answer is all over forums, it doesn't wotk for me. "My" namespace doesn't even contain Computer property. I don't have time to research why is that. Environment.MachineName solved problem for me. – lot Mar 09 '16 at 18:32
0

For WPF application:

  1. Place a Button and TextBlock on the MainWindow.xaml form
  2. Create a name for the button and textblock. Example: TextBlock1, Button1

Code Example #1

Private Sub load(sender As Object, e As EventArgs)Handles MyBase.Loaded

TextBlock1.Text = Environment.MachineName 

End Sub

Code Example #2

Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs) Handles Button1.Click

TextBlock1.Text = Environment.MachineName 

MyBase.Title = "Hello " + Environment.MachineName 

End Sub

Hope this gives you a more understanding on how to show the users Computer Name.

I stopped using Windows Form Applications because they are way to laggy. Now I'm using WPF Applications mainly because its a lot smoother and it has more customizations and coding WPF applications are a lot similar to Windows Form Applications.

Not found
  • 1
  • 2