如何在。net c#中获取计算机名
当前回答
下面给出了获取机器名或计算机名的一些方法
方法1:
string MachineName1 = Environment.MachineName;
方法2:
string MachineName2 = System.Net.Dns.GetHostName();
方法三:
string MachineName3 = Request.ServerVariables["REMOTE_HOST"].ToString();
方法4:-
string MachineName4 = System.Environment.GetEnvironmentVariable("COMPUTERNAME");
欲了解更多,请参阅我的博客
其他回答
试试这个:
string[] computer_name = System.Net.Dns.GetHostEntry(System.Web.HttpContext.Current.Request.ServerVariables["remote_addr"]).HostName.Split(new Char[] { '.' });
return computer_name[0].ToString();
您可以使用Environment.MachineName访问机器名。
System.Environment.MachineName
string name = System.Environment.MachineName;
我为我的web项目设置<p>括号的.InnerHtml为用户的计算机名,执行以下操作:
HTML:
<div class="col-md-4">
<h2>Your Computer Name Is</h2>
<p id="pcname" runat="server"></p>
<p>
<a class="btn btn-default" href="#">Learn more »</a>
</p>
</div>
C#:
using System;
using System.Web.UI;
namespace GetPCName {
public partial class _Default : Page {
protected void Page_Load(object sender, EventArgs e) {
pcname.InnerHtml = Environment.MachineName;
}
}
}
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 为什么Visual Studio 2015/2017/2019测试运行器没有发现我的xUnit v2测试
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 没有ListBox。SelectionMode="None",是否有其他方法禁用列表框中的选择?
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何在iis7应用程序池中设置。net Framework 4.5版本