getcomputername (kernel32)
Last changed: dauth-202.166.147.204

.
Summary

C# Signature:

    [System.Runtime.InteropServices.DllImport("Kernel32")]
    static extern unsafe bool GetComputerName(byte* lpBuffer, long* nSize);

       or

    [System.Runtime.InteropServices.DllImport("Kernel32", CharSet=CharSet.Auto)]
    public static extern bool GetComputerName(StringBuffer buffer, ref uint size);    

User-Defined Types:

None.

Notes:

The GetComputerName function retrieves the NetBIOS name of the local computer. This name is established at system startup, when the system reads it from the registry.

Tips & Tricks:

Make sure to go in to the build properties of your project and check "Allow Unsafe Code". Second version doesn't recquest this.

Sample Code:

    using System.Management;
    using System.Runtime.InteropServices;

    class Class1
    {
        [System.Runtime.InteropServices.DllImport("Kernel32")]
            static extern unsafe bool GetComputerName(byte* lpBuffer, long* nSize);

        [STAThread]
        static void Main(string[] args)
        {
           byte[] buffor = new byte[512];
            long size = buffor.Length;
               unsafe
             {
            long* pSize = &size;
            fixed (byte* pBuffor = buffor)
            {
              GetComputerName(pBuffor, pSize);
            }
                }
           System.Text.Encoding textEnc = new System.Text.ASCIIEncoding();
           System.Console.WriteLine("Computer name: {0}", textEnc.GetString(buffor));         
        }
    }

Alternative Managed API:

System.Environment.MachineName will get you the netbios name.

Documentation