System.Net.NameResolutionPal.GetHostName C# (CSharp) Method

GetHostName() public static method

public static GetHostName ( ) : string
return string
        public static string GetHostName()
        {
            //
            // note that we could cache the result ourselves since you
            // wouldn't expect the hostname of the machine to change during
            // execution, but this might still happen and we would want to
            // react to that change.
            //

            StringBuilder sb = new StringBuilder(HostNameBufferLength);
            SocketError errorCode =
                Interop.Winsock.gethostname(
                    sb,
                    HostNameBufferLength);

            //
            // if the call failed throw a SocketException()
            //
            if (errorCode != SocketError.Success)
            {
                throw new SocketException();
            }
            return sb.ToString();
        }

Usage Example

Example #1
0
        /// <summary>Gets the host name of the local machine.</summary>
        public static string GetHostName()
        {
            NameResolutionPal.EnsureSocketsAreInitialized();

            ValueStopwatch stopwatch = NameResolutionTelemetry.Log.BeforeResolution(string.Empty);

            string name;

            try
            {
                name = NameResolutionPal.GetHostName();
            }
            catch when(LogFailure(stopwatch))
            {
                Debug.Fail("LogFailure should return false");
                throw;
            }

            if (NameResolutionTelemetry.Log.IsEnabled())
            {
                NameResolutionTelemetry.Log.AfterResolution(stopwatch, successful: true);
            }

            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Info(null, name);
            }
            return(name);
        }
All Usage Examples Of System.Net.NameResolutionPal::GetHostName