【问题标题】:How do I get the DNS Suffix Search List programmatically in C#如何在 C# 中以编程方式获取 DNS 后缀搜索列表
【发布时间】:2020-02-06 19:03:43
【问题描述】:

如何使用 C# 在 Windows 10 计算机上获取 DNS 后缀搜索列表?

例如,如果我在 cmd 中输入 ipconfig,我会看到类似:

Windows IP Configuration

   Host Name . . . . . . . . . . . . : BOB
   Primary Dns Suffix  . . . . . . . : fred.george.com
   Node Type . . . . . . . . . . . . : Hybrid
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No
   DNS Suffix Search List. . . . . . : fred.com
                                       george.com

我想要返回一个包含“fred.com”和“george.com”的数组。我尝试了一些不同的东西[1],但它们使用了适配器属性(空白)。

[1]https://docs.microsoft.com/en-us/dotnet/api/system.net.networkinformation.ipinterfaceproperties.dnssuffix?view=netframework-4.8

【问题讨论】:

  • 为什么不直接运行该命令,然后提取这些行?
  • 您可能会找到灵感 here,但我会在这方面使用 BugFinder - 构建正确的列表很复杂,因为信息是动态的而不是静态的。

标签: c# windows dns


【解决方案1】:

域名后缀存储在注册表中:

System\CurrentControlSet\Services\Tcpip\Parameters
SearchList (REG_SZ)

机器的设置是全局的(所有适配器和所有 IP 地址(IPv4 和 IPv6))

使用此代码获取字符串:

string searchList = "";
try
{
    using (var reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(tcpSettingsSubKey))
    {
        searchList = (reg.GetValue("SearchList") as string);
    }
}
catch(Exception ex)
{
    // something went wrong
}

我编写了这个类来获得更多这些设置。在这个类中,后缀存储在DNSSearchListStringDNSSearchList中。

using System;
using System.Linq;

/// <summary>
/// Retrieving some IP settings from the registry.
/// The default dns suffix is not stored there but cat be read from:
/// <see cref="System.Net.NetworkInformation.IPInterfaceProperties.DnsSuffix"/>
/// </summary>
public static class LocalMachineIpSettings
{
    private readonly static object dataReadLock = new object();
    private static bool dataReadFinished = false;

    private static string domain;
    private static string hostname;
    private static int? iPEnableRouter;

    /// <summary>
    /// Search list (the suffixes) as stored in registry
    /// </summary>
    private static string searchListString;

    /// <summary>
    /// Search list (the suffixes) as string array
    /// </summary>
    private static string[] searchList;

    /// <summary>
    /// also available at: <see cref="System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties()"/>
    /// </summary>
    public static string Domain { get { ReadValues(); return domain; } }
    /// <summary>
    /// also available at: <see cref="System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties()"/>
    /// </summary>
    public static string Hostname { get { ReadValues(); return hostname; } }
    public static int? IPEnableRouter { get { ReadValues(); return iPEnableRouter; } }
    public static string[] DNSSearchList { get { ReadValues(); return searchList; } }
    public static string DNSSearchListString { get { ReadValues(); return searchListString; } }

    private static void ReadValues()
    {
        lock (dataReadLock)
        {
            if (dataReadFinished == true)
            {
                return;
                //<----------
            }

            ForceRefresh();
        }
    }

    /// <summary>
    /// Reread the values
    /// </summary>
    public static void ForceRefresh()
    {
        const string tcpSettingsSubKey = @"System\CurrentControlSet\Services\Tcpip\Parameters";

        lock (dataReadLock)
        {
            try
            {
                Microsoft.Win32.RegistryKey reg = null;
                using (reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(tcpSettingsSubKey))
                {
                    domain = (reg.GetValue("Domain") as string);
                    hostname = (reg.GetValue("Hostname") as string);
                    iPEnableRouter = (reg.GetValue("IPEnableRouter") as int?);
                    searchListString = (reg.GetValue("SearchList") as string);
                    searchList = searchListString?.Split(new[] { ' ', ',', ';' }, StringSplitOptions.RemoveEmptyEntries).Select(o => o.Trim(' ', '.')).ToArray();
                }

                dataReadFinished = true;
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Cannot access HKLM\\{ tcpSettingsSubKey } or values beneath see inner exception for details", ex);
                //<----------
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 2018-09-15
    相关资源
    最近更新 更多