/// <summary> /// Extracts the computer name from the passed in dnsName. Note that if the dnsName is an /// IP address, no conversion is performed. Also note that if the dnsName is a FQDN name, the /// extracted computer name is not the same as the NetBIOS name, particularly in the case /// that the computer name is longer than 15 characters (the limit for NetBios names). /// </summary> /// <param name="dnsName">The dnsName.</param> /// <param name="parseOption">Used to specify whether it is ok if the dnsName is not the FQDN name.</param> /// <returns>The extracted computer name.</returns> public static string GetComputerName(string dnsName, DnsParseOption parseOption) { string computerName; if (dnsName.IndexOf('.') == -1) { // No dot in the dnsName - assume computername is same as dnsName computerName = dnsName; if (parseOption == DnsParseOption.RequireFQDN) { throw new Exception(String.Format("Compute name {0} is invalid", dnsName)); } } else { IPAddress ipAddr; if (IPAddress.TryParse(dnsName, out ipAddr)) { // don't try to parse the IP computerName = dnsName; } else { computerName = dnsName.Substring(0, dnsName.IndexOf('.')); } } return(computerName); }
public static string GetDomainName(string dnsName, DnsParseOption parseOption) { string domainName; if (dnsName.IndexOf('.') == -1) { // No dot in the dnsName - assume domain name is the same as the dnsName domainName = dnsName; if (parseOption == DnsParseOption.RequireFQDN) { throw new Exception(String.Format("Computer name {0} is invalid", dnsName)); } } else { IPAddress ipAddr; if (IPAddress.TryParse(dnsName, out ipAddr)) { // there's no domain in case of IP domainName = string.Empty; } else { domainName = dnsName.Substring(dnsName.IndexOf('.') + 1); } } return(domainName); }