How to change the name of a NetworkAdapter in c#?

2020-07-24 05:00发布

问题:

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

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.

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#?

回答1:

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

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.

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>");

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.

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



回答2:

You can use the System.Management assembly and use this class.

Follow the sample here - http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/727c8766-8189-4ad6-956d-958e52b97c05/



回答3:

You can also create a VB.NET dll with the functionality you need and reference and call it from your C# code.

Here is a console app demonstrating the code (I tested and it works :)

Option Explicit On
Module Module1
    Sub Main()

        Const NETWORK_CONNECTIONS = &H31&

        Dim sOldName = "Local Area Connection"
        Dim sNewName = "Network"

        Dim objShell, objFolder, colItems, objItem
        objShell = CreateObject("Shell.Application")
        objFolder = objShell.Namespace(NETWORK_CONNECTIONS)

        colItems = objFolder.Items
        For Each objItem In colItems
            Console.WriteLine(objItem.Name)
            If objItem.Name = sOldName Then
                objItem.Name = sNewName
            End If
            Console.WriteLine(objItem.Name)
        Next
    End Sub

End Module

It prints out:

Local Area Connection

Network