3

Is there any way, other than referencing the Microsoft.VisualBasic.dll (such as as below in .NET 3) to retrieve to Computer Name

new Microsoft.VisualBasic.Devices.ServerComputer().Name
johnc
  • 39,385
  • 37
  • 101
  • 139
  • 1
    While the question is of course valid (and has an answer), I would just like to add that despite the DLL being called ".VisualBasic", it's all .net IL at the end, so there is no penalty in using it. – Michael Stum Mar 17 '10 at 02:13
  • It's also a dupe... http://stackoverflow.com/questions/1768198 – Austin Salonen Mar 17 '10 at 02:15
  • @Michael - I realised that, it just felt funny doing it, that's all :) – johnc Mar 17 '10 at 02:25
  • Apologies for the dupe, I did search. I think this is valid to stay open, as I was, as others have, searching for an alternative to the much used VisualBasic dll reference rather than just how do I get a computer name. A subtle distinction, but probably why I didn't find the answer given above, and others might find this one instead. – johnc Mar 17 '10 at 03:07
  • In this case it's unnecessary to use Microsoft.VisualBasic.dll to find the computer name. But Michael makes a good point: there are useful functions in Microsoft.VisualBasic.dll, and you sometimes see people avoiding using it when it would really help them. For example http://stackoverflow.com/questions/1471918/pv-function-and-porting-vb6-to-c – MarkJ Mar 17 '10 at 10:36

2 Answers2

16

Use System.Environment.MachineName.

Jason Kresowaty
  • 16,105
  • 9
  • 57
  • 84
4

You can use System.Windows.Forms.dll and the SystemInformation class. You can also use System.Environment.MachineName or System.Environment.GetEnvironmentVariable("COMPUTERNAME"). And if you're really devoted, you can make a P/Invoke into user32.dll to call the appropriate Win32 API.

JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169