How can you get the FQDN of a local machine in C#?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
If you want to tidy it up, and handle exceptions, try this:
This is covered by this article. This technique is more brief than the accepted answer and probably more reliable than the next most-voted answer. Note that as far as I understand, this doesn't use NetBIOS names, so it should be suitable for Internet use.
.NET 2.0+
.NET 1.0 - 1.1
Here it is in PowerShell, for the heck of it:
You can try the following:
This shoud give you the FQDN of the current local machine (or you can specify any host).
Used this as one of my options to combine host name and domain name for building a report, added the generic text to fill in when domain name was not captured, this was one of the customers requirements.
I tested this using C# 5.0, .Net 4.5.1
Built using ideas from Miky Dinescu solution.
A slight simplification of Miky D's code