Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A WMI objects sets the computer name. Then the registry is used to check whether the name was set. Because the System.Environment.MachineName is not updated right away. And the 'hostname' command in CMD.exe still outputs the old name. So a reboot is still required. But with the registry check can see if the name was set.</p> <p>Hope this helps.</p> <pre><code>Boolean SetComputerName(String Name) { String RegLocComputerName = @"SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName"; try { string compPath= "Win32_ComputerSystem.Name='" + System.Environment.MachineName + "'"; using (ManagementObject mo = new ManagementObject(new ManagementPath(compPath))) { ManagementBaseObject inputArgs = mo.GetMethodParameters("Rename"); inputArgs["Name"] = Name; ManagementBaseObject output = mo.InvokeMethod("Rename", inputArgs, null); uint retValue = (uint)Convert.ChangeType(output.Properties["ReturnValue"].Value, typeof(uint)); if (retValue != 0) { throw new Exception("Computer could not be changed due to unknown reason."); } } RegistryKey ComputerName = Registry.LocalMachine.OpenSubKey(RegLocComputerName); if (ComputerName == null) { throw new Exception("Registry location '" + RegLocComputerName + "' is not readable."); } if (((String)ComputerName.GetValue("ComputerName")) != Name) { throw new Exception("The computer name was set by WMI but was not updated in the registry location: '" + RegLocComputerName + "'"); } ComputerName.Close(); ComputerName.Dispose(); } catch (Exception ex) { return false; } return true; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload