vb.net 获取计算机名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13388164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the computer name
提问by Nh123
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?
获取计算机名称的快速方法是什么?
回答by famf
you can just use without function:
你可以只使用没有功能:
System.Net.Dns.GetHostName
or:
或者:
Environment.MachineName
回答by Check-Kay Wong
This is not that good the previous anwser,but if you love to work with forms:
这不是以前的 anwser,但如果您喜欢使用表单:
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.
System.Windows.Forms.SystemInformation 提供了很多不错的信息。
-> UserDomainName
-> 用户域名
-> UserName
-> 用户名
回答by lot
There is a nice static property that should work anywhere in your program and in both VB.NET and C#:
有一个很好的静态属性,它可以在您的程序以及 VB.NET 和 C# 中的任何地方工作:
System.Environment.MachineName
Because Systemnamespace is imported automatically by default, it should be enough to refer to it simply by typing Environment.MachineName.
因为System命名空间默认是自动导入的,所以只需输入Environment.MachineName就可以引用它。
For unknown reason My.Computer.Nameis 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.
由于未知原因My.Computer.Name对很多人(包括我)不起作用,尽管它经常被提及为获取当前主机名的正确方法。所以你不需要费心去尝试让它工作。
回答by Rem
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#
仅适用于 VB.NET,不适用于 C#
回答by Not found
For WPF application:
对于 WPF 应用程序:
- Place a Button and TextBlock on the MainWindow.xaml form
- Create a name for the button and textblock. Example: TextBlock1, Button1
- 在 MainWindow.xaml 表单上放置一个 Button 和 TextBlock
- 为按钮和文本块创建一个名称。示例:TextBlock1、Button1
Code Example #1
代码示例 #1
Private Sub load(sender As Object, e As EventArgs)Handles MyBase.Loaded
TextBlock1.Text = Environment.MachineName
End Sub
Code Example #2
代码示例 #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.
我停止使用 Windows 窗体应用程序,因为它们太滞后了。现在我使用 WPF 应用程序主要是因为它更流畅,并且有更多的自定义和编码 WPF 应用程序与 Windows 窗体应用程序非常相似。