本文介绍了如何在 C# 中更改网络适配器的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人们声称以下 VB 脚本可用于更改网络适配器名称.但是,我在尝试将其转换为可以执行相同操作的 c# 应用程序时遇到了明显的困难.我似乎面临的问题是对 NetworkInterface.Name 的调用是只读的.

People claim the following VB script works for changing network adapter names. However I am having a decidedly difficult time trying to convert this to a c# appliaction that can do the same thing. The problem I seem to be facing is that calls to the NetworkInterface.Name is readonly.

Option Explicit

Const NETWORK_CONNECTIONS = &H31&

Dim sOldName= WScript.Arguments(0)
Dim sNewName= WScript.Arguments(1)

Dim objShell, objFolder, colItems, objItem

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(NETWORK_CONNECTIONS)

Set colItems = objFolder.Items
For Each objItem in colItems
    If objItem.Name = sOldName Then
        objItem.Name =sNewName
    End If
Next

我发现这个解释了更多:http://blogs.technet.com/b/heyscriptingguy/archive/2005/05/11/how-can-i-rename-a-local-area-连接.aspx.

I found this which explains it a bit more: http://blogs.technet.com/b/heyscriptingguy/archive/2005/05/11/how-can-i-rename-a-local-area-connection.aspx.

好的,所以有一些特殊的文件夹来存储 NIC 名称,您可以通过 SHELL 绑定到这些文件夹来访问这些文件夹.那么你如何在 c# 中做这样的事情?

Ok, so there are special folders where the NIC names are stored and you access those folders by binding to the them via the SHELL. How then do you do something like this in c#?

推荐答案

如果您知道注册表结构的工作原理,您可以通过注册表轻松更改 NIC 的名称.

You can change the name of a NIC easily through the registry if you know how the registry structure works.

您将需要 NetworkAdapters GUID 才能找到要打开的路径.要获得网络适配器 GUID,我建议首先查询 WMIWin32_NetworkAdapter"类.有一个 GUID 属性以及标识特定适配器所需的所有其他属性.

You will need the NetworkAdapters GUID in order to locate which path to open. To get the network adapter GUID I recommend first querying the WMI "Win32_NetworkAdapter" class. There is a GUID property along with all the other properties needed to identify specific adapters.

您会在注册表路径中注意到此 GUID:{4D36E972-E325-11CE-BFC1-08002BE10318}
访问链接了解相关信息:http://technet.microsoft.com/en-us/library/cc780532(v=ws.10).aspx

You will notice this GUID in the registry path: {4D36E972-E325-11CE-BFC1-08002BE10318}
Visit link for information on it:http://technet.microsoft.com/en-us/library/cc780532(v=ws.10).aspx

string fRegistryKey = string.Format(@"SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{0}\Connection", NIC_GUID);

RegistryKey RegistryKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, @"\\" + Server.Name);

RegistryKey = RegistryKey.OpenSubKey(fRegistryKey, true); //true is for WriteAble.

RegistryKey.SetValue("Name", "<DesiredAdapterName>");

Windows UI 的设计不允许出现重复的 NIC 名称.但是,您可以通过注册表强制使用重复的 NIC 名称.我们已经做了测试,重名似乎没有任何严重影响.Windows 似乎仍然可以正常工作.如果您不包含反重复名称逻辑,您只是想警惕针对 NIC 名称的脚本.

By design the windows UI will not allow for duplicate NIC names. However, you can force duplicate NIC names via the registry. We have done tests, there seem to be nothing critically effected by having duplicate names. Windows seems to still function fine. You just want to be wary about scripting against NIC names if you don’t incorporate anti-duplicate name logic.

要创建唯一性,您可以使用与 WMI 查询关联的适配器索引属性.

To create uniqueness you can use the adapter index property associated with the WMI query.

这篇关于如何在 C# 中更改网络适配器的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 21:54