3

There is a property in .NET System.Environment.MachineName which read the name of a computer cropped up to the 15 characters because of compatibility with Windows 98.

How can I get full (not cropped) name?

Tomas Kubes
  • 20,134
  • 14
  • 92
  • 132

2 Answers2

2

I suggest using interop

using System.Runtime.InteropServices;
...

[DllImport("KERNEL32.dll", CharSet = CharSet.Auto, BestFitMapping = false)]
private extern static int GetComputerName(
  [Out]StringBuilder nameBuffer, 
  ref int bufferSize);
...

int size = 0; // do not try to return any name, but its actual size only

// What's actual size of the machine name?
GetComputerName(null, ref size);

// Obtaining the machine name 
StringBuilder buffer = new StringBuilder(size);
GetComputerName(buffer, ref size);

string name = buffer.ToString();
Dmitry Bychenko
  • 149,892
  • 16
  • 136
  • 186
1

You can also use WMI to get the name of the system

using System.Management;

try
        {
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("root\\CIMV2",
                "SELECT Name FROM Win32_ComputerSystem");

            foreach (ManagementObject queryObj in searcher.Get())
            {
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Win32_ComputerSystem instance");
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Name: {0}", queryObj["Name"]);
            }
        }
        catch (ManagementException e)
        {
            // exception handling
        }
Venkatesh
  • 324
  • 2
  • 11