본문 바로가기

.NET/C# Basic

[C#] 하드시리얼넘버, 외부IP, 내부IP, DNS , NetBios, GUID 그위드 , 클릭온스 버전, 외부아이피 내부아이피 구하기 메서드

반응형
 public static class SystemClass
  {
      // 하드시리얼넘버
        public static string GetHDDSerialNumber(string drive)
        {
            if (drive == "" || drive == null)
            {
                drive = "C";
            }
            ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + drive + ":\"");
            disk.Get();

            return disk["VolumeSerialNumber"].ToString();
        }
     // 외부 아이피 
        public static string GetIP()
        {
            string tempIp = string.Empty;
            try
            {
                tempIp = new WebClient().DownloadString("http://www.whatismyip.com/automation/n09230945.asp");
            }
            catch (WebException)
            {
                try
                {
                    tempIp = GetExternalIp().ToString();
                }
                catch (Exception)
                {
                    MessageBox.Show("아이피를 찾지 못했습니다");
                }
            }
            return tempIp;
        }

        public static IPAddress GetExternalIp()
        {
            string checkip = "http://checkip.dyndns.org/";
            WebClient wc = new WebClient();
            UTF8Encoding utf8 = new UTF8Encoding();
            string requestHtml = "";
            try
            {
                requestHtml = utf8.GetString(wc.DownloadData(checkip));
                requestHtml = requestHtml.Substring(75).Remove(requestHtml.Substring(75).IndexOf("</body>")).Trim();
            }
            catch (WebException we)
            {
                // do something with exception
                Console.Write(we.ToString());
            }

            IPAddress externalIp = IPAddress.Parse(requestHtml);
            return externalIp;
        }
       // DNS
        public static string GetDNS()
        {
            return System.Net.Dns.GetHostByName("LocalHost").HostName;
        }
       //NetBios
        public static string GetNetBios()
        {
            return System.Environment.MachineName;
        }
        // 내부 아이피
        public static string GetInnerIp()
        {
            IPAddress ipaddress = Dns.Resolve(Dns.GetHostName()).AddressList[0];
            return ipaddress.ToString();
        }
        // 내부 아이피 전부
        public static List<string> GetInnerIpAll()
        {
            IPHostEntry hostEntry = Dns.GetHostByName(Dns.GetHostName());
            List<string> list = new List<string>();

            for (int i = 0; i < hostEntry.AddressList.Length; i++)
            {
                list.Add(hostEntry.AddressList[i].ToString());
            }
            return list;
        }
         // 그위드
        public static string GetGUID()
        {
            Guid guid = Guid.NewGuid();
            return guid.ToString().ToUpper();
        }
         // 클릭온스버전
         public static string GetSmartVersion()
        {
            if (ApplicationDeployment.IsNetworkDeployed)
            {
                Version myVersion = ApplicationDeployment.CurrentDeployment.CurrentVersion;
                return string.Format("{0}.{1}.{2}.{3} Build", myVersion.Major, myVersion.Minor, myVersion.Build, myVersion.Revision);
            }
            else return string.Empty;
        }
     }
}