Blankman Blankman - 2 years ago 105
C# Question

Get the computer name in a Windows service?

In a .NET Windows service (C#), how can I get the computer name?

Is this a reliable method, or should I wrap it in a try/catch?

Answer Source

Look at the Environment class. There're lots of nice things in there, including the MachineName:

string CurrentMachineName = Environment.MachineName;

According to the docs, this could generate an InvalidOperationException so you'll need to be aware of that possibility. The risk probably doesn't warrant wrapping it in a try/catch, though.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download