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?
you can just use without function:
System.Net.Dns.GetHostName
or:
Environment.MachineName
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
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.
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#
For WPF application:
- Place a Button and TextBlock on the MainWindow.xaml form
- 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.