English 中文(简体)
How can one change a Windows 2000 computer name in .NET 2.0?
原标题:

I m trying to change a computer s name (host name) on Windows 2000 using .NET 2.0. The computer is not joined to a domain.

Windows XP and higher provides the WMI method Win32_ComputerSystem.Rename, but this is not available in Windows 2000 (reference here).

I m not averse to just calling an external program if I need to, but I also can t seem to find one that works on Windows 2000. Searching on Google didn t seem to turn up anything obvious.

Thanks in advance.

最佳回答

I think the Windows API might be of help on Windows 2000: Use SetComputerNameEx:

BOOL WINAPI SetComputerNameEx(
  __in  COMPUTER_NAME_FORMAT NameType,
  __in  LPCTSTR lpBuffer
);

This sample is based on the sample on pinvoke.net:

public class RenameComputer
{
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    static extern bool SetComputerNameEx(COMPUTER_NAME_FORMAT NameType, string lpBuffer);

    enum COMPUTER_NAME_FORMAT
    {
        ComputerNameNetBIOS,
        ComputerNameDnsHostname,
        ComputerNameDnsDomain,
        ComputerNameDnsFullyQualified,
        ComputerNamePhysicalNetBIOS,
        ComputerNamePhysicalDnsHostname,
        ComputerNamePhysicalDnsDomain,
        ComputerNamePhysicalDnsFullyQualified,
    }

    //ComputerNamePhysicalDnsHostname used to rename the computer name and netbios name before domain join
    public static bool Rename(string name)
    {
        bool result = SetComputerNameEx(COMPUTER_NAME_FORMAT.ComputerNamePhysicalDnsHostname, name);
        if (!result)
            throw new Win32Exception();

        return result;
    }
}

In addition to p-invoking the WinAPI you might also use Process.Start in combination with the netsh command as described here.

问题回答

There is an example in C under that link. You could pinvoke that





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签