프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / CommonLib / DNSHelper.cs @ cdfb57ff

이력 | 보기 | 이력해설 | 다운로드 (2.03 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Net;
5
using System.Net.NetworkInformation;
6
using System.Text;
7
using System.Threading.Tasks;
8

    
9
namespace CommonLib
10
{
11
    public class DNSHelper
12
    {
13
        public static IPAddress GetDnsAdress()
14
        {
15
            NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
16

    
17
            foreach (NetworkInterface networkInterface in networkInterfaces)
18
            {
19
                if (networkInterface.OperationalStatus == OperationalStatus.Up)
20
                {
21
                    IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
22
                    IPAddressCollection dnsAddresses = ipProperties.DnsAddresses;
23

    
24
                    foreach (IPAddress dnsAdress in dnsAddresses)
25
                    {
26
                        return dnsAdress;
27
                    }
28
                }
29
            }
30
            return null;
31
        }
32

    
33
        public static async Task<IPHostEntry> GetHostEntryAsync()
34
        {
35
            IPHostEntry result = null;
36

    
37
            try
38
            {
39
                var ipaddress = CommonLib.DNSHelper.GetDnsAdress();
40

    
41
                await Task.Factory.StartNew(() =>
42
                {
43
                    result = Dns.GetHostEntry(ipaddress);
44
                });
45
            }
46
            catch (Exception ex)
47
            {
48
                System.Diagnostics.Debug.WriteLine(ex.ToString());
49
            }
50

    
51
            return result;
52
        }
53

    
54
        public static IPHostEntry GetHostEntryTask()
55
        {
56
            IPHostEntry result = null;
57

    
58
            try
59
            {
60
                var ipaddress = CommonLib.DNSHelper.GetDnsAdress();
61

    
62
                var task = Task.Factory.StartNew(() =>
63
                {
64
                    result = Dns.GetHostEntry(ipaddress);
65
                });
66

    
67
                task.Wait();
68
            }
69
            catch (Exception ex)
70
            {
71
                System.Diagnostics.Debug.WriteLine(ex.ToString());
72
            }
73

    
74
            return result;
75
        }
76
    }
77
}
클립보드 이미지 추가 (최대 크기: 500 MB)