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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Look at the Environment class. There’re lots of nice things in there, including the 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.