I am trying to change the ComputerName on a windows machine. I am having a very strange result.
The Code that I'm using is:
public class MachineService
{
[DllImport("Kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetComputerNameA([MarshalAs(UnmanagedType.LPStr)] string computerName);
[DllImport("Kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetComputerName([MarshalAs(UnmanagedType.LPStr)] string computerName);
[DllImport("Kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetComputerNameEx(ComputerNameFormat NameType, [MarshalAs(UnmanagedType.LPStr)] string computerName);
public static bool ChangeName(string name)
{
var envVar = SetComputerName(name);
var ex = SetComputerNameEx(ComputerNameFormat.ComputerNamePhysicalNetBIOS, name);
return envVar && ex;
}
}
It does set the Environmental Variable ComputerName but it doesn't set the name of the device. please see the images below.
EDIT It has changed the registry.
If I read the documentation correctly you should:
ComputerNamePhysicalDnsHostname
instead of ComputerNamePhysicalNetBIOS
SetComputerName
before SetComputerNameEx