2017-11-13 8 views
0

ipconfig/allからこのsnippitから代替DNSサーバを抽出しようとしています。基本的に、私はDNS ServersラインとNetBIOS over TcpipラインIP設定から代替DNSサーバを解析する

Ethernet adapter Local Area Connection 2: 

    Connection-specific DNS Suffix . : 
    Description . . . . . . . . . . . : Realtek PCIe GBE Family Controller #2 
    Physical Address. . . . . . . . . : 00-0B-AB-37-2D-28 
    DHCP Enabled. . . . . . . . . . . : Yes 
    Autoconfiguration Enabled . . . . : Yes 
    Link-local IPv6 Address . . . . . : fe80::edd5:2e2e:6979:5b59%3(Preferred) 
    Autoconfiguration IPv4 Address. . : 169.254.91.89(Preferred) 
    Subnet Mask . . . . . . . . . . . : 255.255.0.0 
    Default Gateway . . . . . . . . . : 
    DHCPv6 IAID . . . . . . . . . . . : 67111851 
    DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-18-B0-7D-38-00-0B-AB-2E-90-1E 
    DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1 
             fec0:0:0:ffff::2%1 
             fec0:0:0:ffff::3%1 
    NetBIOS over Tcpip. . . . . . . . : Enabled 

ここでは、プライマリDNSサーバーを取得するための私のコードだとの間のすべての行を書きたいです。

string[] lines = System.IO.File.ReadAllLines(@"C:\Temp\ipconfig.txt"); 
foreach (string line in lines) 
    { 
     if (line.Contains("DNS Servers") 
     { 
      txtPreferredDNS = line.Split(':').First(); 
     } 
     // need help getting the alternate DNS servers 
    } 
+1

これを試している理由はありますか?あなたは[C#コードでそれらを手に入れることができます](https://stackoverflow.com/questions/2906706/how-do-i-get-my-current-dns-server-in-c)。 – nvoigt

答えて

1

私はこのような何かをするだろう:

1.- DNSサーバーが表示されたインデックスを見つける:

https://msdn.microsoft.com/en-us/library/system.array.indexof(v=vs.110).aspx

2.-はインデックス

int index = lines.IndexOf('DNS Servers%') 
while (int i=index, i< lines.length, i++){ 
if (!line.Contains("DNS Servers")) 
     { 
      txtPreferredDNS = line.Split(':').First(); // make it an array 
instead or a collection 
     } 
else { 
    //change value of i to get out of the loop 
} 

} 
を知るに

あなたの主な問題は、あなたの例の4つのDNSサーバがそうでない時には同じ行に再書いてください。

また、正規表現を使用して使用することもできます。これは、より良い解決策になります。

関連する問題