在Windows服务中获取计算机名称? [英] Get the computer name in a Windows service?

查看:157
本文介绍了在Windows服务中获取计算机名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET Windows服务(C#)中,如何获取计算机名称?

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

这是一种可靠的方法,还是应该尝试将其包装起来/ catch?

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

推荐答案

查看环境类。那里有很多美好的事物,包括 MachineName

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

string CurrentMachineName = Environment.MachineName;

根据文档,这可能会产生InvalidOperationException,因此您需要意识到这种可能性。不过,这种风险可能不保证将其包装在try / catch中。

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.

这篇关于在Windows服务中获取计算机名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆