大网数据广告活动
服务器运维问答

How to change network adapter DNS servers on Windows without using netsh or WIM

· 52CE 我爱测

I would like to modify network adapter DNS servers on Windows programmatically. I don't want to use WMI, because it fails for instance when no network cable is connected to the ethernet adapter being modified. What's more, it doe…

问题原文

原帖链接 · Mariusz Schimke · CC BY-SA 4.0

I would like to modify network adapter DNS servers on Windows programmatically. I don't want to use WMI, because it fails for instance when no network cable is connected to the ethernet adapter being modified. What's more, it does not support setting IPv6 configuration.

Therefore I attempted to set DNS servers directly in the system registry:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip[6]\Parameters\Interfaces\[Interface GUID]\NameServer

Unfortunately, these settings are not applied instantly.

Netsh command seems to do what I want perfectly, but honestly, I think it would be better if I could stay away from starting external processes from the application I build. I would like it to be independent. I'm wondering if there is a way to do that. Maybe some other registry changes should be modified as well, or some action should be triggered by WMI?

已采纳解答

原帖链接 · Florian Rossmark · CC BY-SA 4.0

NETSH is still what you want to do. You seem to have developed some kind of application, but altering the registry is sure not the way to go.

Besides that - I highly recommend you use DHCP and reservation rather then hard coding any IP settings every on a server / client - besides those few that have to be (DHCP servers, Domain Controllers e.g.).

DHCP allows you to centrally manage those settings and roll out to the DHCP clients (Client/Server OS) without ever touching them..

Your only other option would be PowerShell - but again - you don't seem to want to use "external" programs, though - NETSH is a part of Windows, only that it is a command line tool (more or less).

You don't mention much about the developing platform you are using - but there is another chance that e.g. a .NET FrameWork provider lets you enumerate and change the settings of network cards, I never looked in to that specifically, but I could imagine there is a chance you can accomplish this..

And if you are afraid NETSH would fail - well - you can still execute it and afterwards double check the registry if the expected settings had been applied..

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-01-29)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。