从一个窗口服务获得计算机名 [英] get computer name from within a windows service

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

问题描述

在一个.NET Windows服务(C#),我怎么能得到计算机名称?

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

这是一个可靠的方法,或者我应该把它包在try / catch语句?

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

推荐答案

看的环境类。那里有很多的好东西在里面,包括<一href="http://msdn.microsoft.com/en-us/library/system.environment.machinename(VS.80).aspx">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.

这篇关于从一个窗口服务获得计算机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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