none
How to get changed Computer Name without restarting the computer RRS feed

  • Вопрос

  • Hi,
     
    I changed a computer from "A" to "B" . I don't restart the computer. When I use System.Environment.MachineName I get "A" and not "B" - which correct. But when I see " System Property page" , " Computer Name tab " I see computer name as "B".

    Where does "B" get saved in Windows memory and how do I access that value.

    Please can somebody help me on this.
    10 сентября 2008 г. 13:02

Ответы

  • I got the solution for this. The pending machine name get stored in System Registry we can access that value programmatically. Please find the code below


    Code to access Pending Computer Name

    Dim skey As RegistryKey

    skey = Registry.LocalMachine.OpenSubKey("SYSTEM\ControlSet001\Control\ComputerName\ComputerName", True)

    Dim svalue As String

    svalue = skey.GetValue("ComputerName")

    MessageBox.Show(svalue)

    Code to access Active Computer Name (just on line)

    MessageBox.Show(Environment.MachineName)

    • Помечено в качестве ответа F Sayed 11 сентября 2008 г. 11:05
    11 сентября 2008 г. 11:05

Все ответы

  • The system environment variables are cached at system startup and do not change until the system reboots.  Windows requires that you reboot before the computer name is formally changed.  Therefore the behavior of any API you use to get the computer name while a reboot is pending is an implementation detail.

    The standard mechanism for getting a computer's name is to use GetComputerName.  This API retrieves the NetBIOS name from the registry and is cached for the duration of the system.

    What you are wanting is the pending machine name.  I don't know that this information is accessible via any published API.  It is pending so it doesn't really help you any as it has no useful purpose until the reboot is complete.  The only way you'll likely be able to get the information that System displays is by using an undocumented function or relying on an implementation detail such as the path in the registry where the machine name is stored across sessions.  I suspect that System queries this entry directly.

    Michael Taylor - 9/10/08
    http://p3net.mvps.org

    10 сентября 2008 г. 17:49
    Модератор
  • I got the solution for this. The pending machine name get stored in System Registry we can access that value programmatically. Please find the code below


    Code to access Pending Computer Name

    Dim skey As RegistryKey

    skey = Registry.LocalMachine.OpenSubKey("SYSTEM\ControlSet001\Control\ComputerName\ComputerName", True)

    Dim svalue As String

    svalue = skey.GetValue("ComputerName")

    MessageBox.Show(svalue)

    Code to access Active Computer Name (just on line)

    MessageBox.Show(Environment.MachineName)

    • Помечено в качестве ответа F Sayed 11 сентября 2008 г. 11:05
    11 сентября 2008 г. 11:05
  • You should be very careful with this code.  Firstly you are basing your code on an implementation detail.  It can change at any time.  I don't really understand why you need to know the future machine name as it has no meaning until the system reboots.

    The second issue is that ControlSet001 is actually part of a set of cloned keys.  The CurrentControlSet tree is generated from one of the ControlSet### trees during boot based upon the configuration the user elected to load.  In most cases ControlSet001 is the likely one to be replicated but this is guaranteed and, again, is an implementation detail.  In the registry under HKLM\SYSTEM\Select is the # of the tree that is actually being used currently and the one that will be used during boot to generate CurrentControlSet.  If you want to be as accurate as possible in using an undocumented behavior then you should query this key to get the ControlSet### tree that will be used when Windows boots again.

    Michael Taylor - 9/11/08
    http://p3net.mvps.org
    11 сентября 2008 г. 13:05
    Модератор
  • Hi F Sayed,

    Is this possible to access Active Computer Name as well as Pending computer name? 

    If yes, request you to please send a code for CMD or powershell.

    Thanks in advance.

    Regards,

    Krishan.Kumar

    30 апреля 2020 г. 13:26
  • $Env:ComputerName | Select-Object

    Or if you use PS Core:

    Invoke-Expression -Command 'hostname'

    Note: "hostname" command is available on both Windows and *nix systems.


    Отвечающий