Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

SetComputerName (kernel32)
 
.
Summary
Sets a NetBIOS or DNS name for the local computer.
Summary

C# Signature:

[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern bool SetComputerNameEx(COMPUTER_NAME_FORMAT NameType,
   string lpBuffer);
[DllImport("kernel32.dll")]
static extern bool SetComputerName(string lpComputerName);

User-Defined Types:

COMPUTER_NAME_FORMAT

Notes:

None.

Tips & Tricks:

Please add some!

VB.NET Signature:

<DllImport("kernel32.dll")> _
Private Shared Function SetComputerName(ByVal lpComputerName As String) As Boolean
End Function

User-Defined Types:

None.

Sample Code:

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

Notes:

You need to restart your computer for the new computer name to take effect.

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

Tips & Tricks:

Please add some!

    //ComputerNamePhysicalDnsHostname used to rename the computer name and netbios name before domain join
    public static bool RenameComputer(string name)
    {
        try
        {
        return SetComputerNameEx(COMPUTER_NAME_FORMAT.ComputerNamePhysicalDnsHostname, name);
        }
        catch (Exception)
        {
        return false;
        }
    }

C# Sample Code:

using System.Runtime.InteropServices;

Alternative Managed API:

Do you know one? Please contribute it!

    class Program
    {
    [DllImport("kernel32.dll")]
    static extern bool SetComputerName(string lpComputerName);

Documentation

    static void Main(string[] args)
    {
        bool done = SetComputerName("New Comp Name");

        if (done)
        {
        Console.WriteLine("Done");
        }

        Console.ReadKey();
    }
    }

VB.NET Sample Code:

Imports System.Runtime.InteropServices

Class Program

    <DllImport("kernel32.dll")> _
    Private Function SetComputerName(ByVal lpComputerName As String) As Boolean
    End Function

    Private Shared Sub Main(args As String())

        Dim done As Boolean = SetComputerName("New Comp Name")

        If done Then
        Console.WriteLine("Done")
        End If

        Console.ReadKey()

    End Sub

End Class

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

http://msdn2.microsoft.com/en-us/library/ms724930.aspx

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions