首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在C#中以编程方式获取DNS后缀搜索列表

如何在C#中以编程方式获取DNS后缀搜索列表
EN

Stack Overflow用户
提问于 2019-10-09 20:31:48
回答 1查看 716关注 0票数 2

如何在C#中获取Windows10机器上的DNS后缀搜索列表?

例如,如果我在cmd中键入ipconfig,我会看到类似以下内容:

代码语言:javascript
运行
复制
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’的数组。我尝试了几种不同的适配器,但它们都使用things1属性(这些属性是空的)。

1

EN

Stack Overflow用户

回答已采纳

发布于 2020-10-07 00:39:37

域后缀存储在注册表中:

代码语言:javascript
运行
复制
System\CurrentControlSet\Services\Tcpip\Parameters
SearchList (REG_SZ)

这些设置是针对计算机的全局设置(所有适配器和所有ip地址(IPv4和IPv6))

使用以下代码获取字符串:

代码语言:javascript
运行
复制
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中。

代码语言:javascript
运行
复制
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);
                //<----------
            }
        }
    }
}
票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58304199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档