⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ping_getservername.cs

📁 本未例说明如何通过域名转为IP地址,并连接域名上的SQL Server
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;

namespace PingDomain
{
    class Ping_GetServerName
    {
        private string _domainName;
        public string DomainName
        {
            get
            {
                return _domainName;
            }
            set
            {
                _domainName = value;
            }
        }


        public Ping_GetServerName()
        { 
        }

        public Ping_GetServerName(string iDomainName)
        {
            DomainName = iDomainName;
        }

        public static PingReply GetPingReply(string iDomainName)
        {

            try
            {

                Ping pingSender = new Ping();
                PingOptions options = new PingOptions();

                options.DontFragment = true;

                string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
                byte[] buffer = Encoding.ASCII.GetBytes(data);
                int timeout = 120;

                PingReply reply = pingSender.Send(iDomainName, timeout, buffer, options);

                return reply;
            }
            catch (Exception ex)
            {
                throw ex;// new Exception("未能正确解析此域名");
            }
        }

        public static string GetIPAddress(string iDomainName)
        {
            PingReply reply = GetPingReply(iDomainName);
            if (reply.Status != IPStatus.Success)
            {
                throw new Exception("未能正确获取IP地址");
            }

            return reply.Address.ToString();
        }

        public PingReply GetPingReply()
        { 
            PingReply reply = GetPingReply(this.DomainName );
            return reply;            
        }

        public string GetIPAddress()
        { 
            PingReply reply = GetPingReply(this.DomainName );
            if (reply.Status != IPStatus.Success)
            {
                throw new Exception("未能正确获取IP地址");
            }


            return reply.Address.ToString();            
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -