用C#如何获得本地电脑的计算机名 如题。C#基于WINFORM的应用程序,如何获得本地电脑的计算机名,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 MessageBox.Show(SystemInformation.ComputerName); MessageBox.Show(SystemInformation.ComputerName); using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;using System.Net;using System.Net.Sockets;using System.DirectoryServices;namespace GetIPMac{ //private System.ComponentModel.Container components = null; public partial class MainForm : Form { public MainForm() { InitializeComponent(); } [DllImport("Iphlpapi.dll")] private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length); [DllImport("Ws2_32.dll")] private static extern Int32 inet_addr(string ip); private string macAddress,ipAddress; private bool canPing = false; private string GetMacAddress() // 得到指定IP的MAC地址 { Int32 ldest = 0; try { ldest = inet_addr(ipAddress); } catch (Exception iperr) { MessageBox.Show(iperr.Message); } Int64 macinfo = new Int64(); Int32 len = 6; try { int res = SendARP(ldest, 0, ref macinfo, ref len); } catch (Exception err) { // throw new Exception("在解析MAC地址过程发生了错误!"); MessageBox.Show(err.Message); } string originalMACAddress = macinfo.ToString("X4"); if (originalMACAddress != "0000" && originalMACAddress.Length == 12) { //合法MAC地址 string mac1, mac2, mac3, mac4, mac5, mac6; mac1 = originalMACAddress.Substring(10, 2); mac2 = originalMACAddress.Substring(8, 2); mac3 = originalMACAddress.Substring(6, 2); mac4 = originalMACAddress.Substring(4, 2); mac5 = originalMACAddress.Substring(2, 2); mac6 = originalMACAddress.Substring(0, 2); macAddress = mac1 + "-" + mac2 + "-" + mac3 + "-" + mac4 + "-" + mac5 + "-" + mac6; canPing = true; } else { macAddress = "无法探测到MAC地址"; canPing = false; } return macAddress; } private void tbar_ButtonClick(object sender, ToolBarButtonClickEventArgs e) { switch (e.Button.Name) { case "Btn_search": int i = 0; DirectoryEntry root = new DirectoryEntry("WinNT:"); DirectoryEntries domains = root.Children; domains.SchemaFilter.Add("domain"); foreach (DirectoryEntry domain in domains) { this.g_list.Items.Add(domain.Name.ToString()); DirectoryEntries computers = domain.Children; computers.SchemaFilter.Add("computer"); foreach (DirectoryEntry computer in computers) { this.Grid_view.Rows.Add(); this.Grid_view.Rows[i].Cells[0].Value = i + 1; this.Grid_view.Rows[i].Cells[1].Value = computer.Name.ToString(); IPHostEntry iphe = null; try { iphe = Dns.GetHostEntry(computer.Name.ToString()); this.Grid_view.Rows[i].Cells[2].Value = iphe.AddressList[0].ToString(); ipAddress = iphe.AddressList[0].ToString(); this.Grid_view.Rows[i].Cells[3].Value = GetMacAddress(); } catch { continue; } this.Grid_view.Rows[i].Cells[4].Value = domain.Name.ToString(); i++; } } break; } } private void button1_Click(object sender, EventArgs e) { int i = 0; DirectoryEntry root = new DirectoryEntry("WinNT:"); DirectoryEntries domains = root.Children; domains.SchemaFilter.Add("domain"); Application.DoEvents(); foreach (DirectoryEntry domain in domains) { this.g_list.Items.Add(domain.Name.ToString()); DirectoryEntries computers = domain.Children; computers.SchemaFilter.Add("computer"); foreach (DirectoryEntry computer in computers) { this.Grid_view.Rows.Add(); this.Grid_view.Rows[i].Cells[0].Value = i + 1; this.Grid_view.Rows[i].Cells[1].Value = computer.Name.ToString(); IPHostEntry iphe = null; try { iphe = Dns.GetHostEntry(computer.Name.ToString()); this.Grid_view.Rows[i].Cells[2].Value = iphe.AddressList[0].ToString(); ipAddress = iphe.AddressList[0].ToString(); this.Grid_view.Rows[i].Cells[3].Value = GetMacAddress(); } catch { continue; } this.Grid_view.Rows[i].Cells[4].Value = domain.Name.ToString(); i++; } } } }} namespace GetIPMac{ partial class MainForm { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.Grid_view = new System.Windows.Forms.DataGridView(); this.button1 = new System.Windows.Forms.Button(); this.g_list = new System.Windows.Forms.ListBox(); this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Column5 = new System.Windows.Forms.DataGridViewTextBoxColumn(); ((System.ComponentModel.ISupportInitialize)(this.Grid_view)).BeginInit(); this.SuspendLayout(); // // Grid_view // this.Grid_view.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.Grid_view.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.Column1, this.Column2, this.Column3, this.Column4, this.Column5}); this.Grid_view.Location = new System.Drawing.Point(139, 28); this.Grid_view.Name = "Grid_view"; this.Grid_view.RowTemplate.Height = 23; this.Grid_view.Size = new System.Drawing.Size(515, 266); this.Grid_view.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(533, 300); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(59, 26); this.button1.TabIndex = 1; this.button1.Text = "搜索"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // g_list // this.g_list.FormattingEnabled = true; this.g_list.ItemHeight = 12; this.g_list.Location = new System.Drawing.Point(13, 28); this.g_list.Name = "g_list"; this.g_list.Size = new System.Drawing.Size(120, 268); this.g_list.TabIndex = 2; // // Column1 // this.Column1.HeaderText = "编号"; this.Column1.Name = "Column1"; // // Column2 // this.Column2.HeaderText = "机器名"; this.Column2.Name = "Column2"; // // Column3 // this.Column3.HeaderText = "IP地址"; this.Column3.Name = "Column3"; // // Column4 // this.Column4.HeaderText = "Mac地址"; this.Column4.Name = "Column4"; // // Column5 // this.Column5.HeaderText = "工作组"; this.Column5.Name = "Column5"; // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(682, 338); this.Controls.Add(this.g_list); this.Controls.Add(this.button1); this.Controls.Add(this.Grid_view); this.Name = "MainForm"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.Grid_view)).EndInit(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.DataGridView Grid_view; private System.Windows.Forms.Button button1; private System.Windows.Forms.ListBox g_list; private System.Windows.Forms.DataGridViewTextBoxColumn Column1; private System.Windows.Forms.DataGridViewTextBoxColumn Column2; private System.Windows.Forms.DataGridViewTextBoxColumn Column3; private System.Windows.Forms.DataGridViewTextBoxColumn Column4; private System.Windows.Forms.DataGridViewTextBoxColumn Column5; }}以是是获取局域网跟你一个工作组的计算机信息的 MachineName 获取此本地计算机的 NetBIOS 名称。 ComputerName 获取本地计算机的 NetBIOS 计算机名称。 private void GetIPAndMacAndHddAndLocalUser() { //获取本机IP string[] IP = new string[C_Common.GetLocalMachineInfo("IP").Length]; IP = C_Common.GetLocalMachineInfo("IP"); for (int i = 0; i < IP.Length; i++) { BRLibrary.Parameter.strLoaclIP = BRLibrary.Parameter.strLoaclIP + IP[i].ToString(); } //获取本机MAC string[] Mac = new string[C_Common.GetLocalMachineInfo("MAC").Length]; Mac = C_Common.GetLocalMachineInfo("MAC"); for (int j = 0; j < Mac.Length; j++) { BRLibrary.Parameter.strLocalMAC = BRLibrary.Parameter.strLocalMAC + Mac[j]; } if (string.IsNullOrEmpty(BRLibrary.Parameter.strLocalMAC) == false) { BRLibrary.Parameter.strLocalMAC = BRLibrary.Parameter.strLocalMAC.Substring(0, 12); } //获取本机用户 string[] User = new string[C_Common.GetLocalMachineInfo("USER").Length]; User = C_Common.GetLocalMachineInfo("USER"); for (int k = 0; k < User.Length; k++) { BRLibrary.Parameter.strLocalUser = BRLibrary.Parameter.strLocalUser + User[k]; } }public string[] GetLocalMachineInfo(string Type) { switch (Type.ToUpper()) { case "IP": IPHostEntry IPHE = new IPHostEntry(); IPHE = Dns.GetHostEntry(Dns.GetHostName()); string[] resultIP = new string[IPHE.AddressList.Length]; for (int i = 0; i < IPHE.AddressList.Length; i++) { resultIP[i] = IPHE.AddressList[i].ToString(); } return resultIP; break; case "MAC": ManagementClass mc; ManagementObjectCollection moc; mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); moc = mc.GetInstances(); int j = 0; foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { j = j + 1; } } string[] resultMac = new string[j]; j = 0; foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { resultMac[j] = mo["MacAddress"].ToString().Replace(":", ""); j = j + 1; } } return resultMac; break; case "HDD": ManagementObject disk; disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\""); disk.Get(); string[] resultHDD = new string[1]; resultHDD[0] = disk.GetPropertyValue("VolumeSerialNumber").ToString(); return resultHDD; break; case "USER": ManagementClass mcUser = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection mocUser = mcUser.GetInstances(); int k = 0; foreach (ManagementObject mo in mocUser) { k = k + 1; } string[] resultUser = new string[k]; k = 0; foreach (ManagementObject mo in mocUser) { resultUser[k] = mo["UserName"].ToString(); k = k + 1; } return resultUser; break; default: string[] resultNull = new string[1]; return resultNull; break; } } SystemInformation.ComputerName这个就可以 MessageBox.Show(SystemInformation.ComputerName); 在C# 底下 大家都是怎么完成指标这样概念的 C# dataGirlView控件 winform窗体之间控件的传递问题 求助,C#写FTP下载的时候,每每都是剩下几K下不到…… c#学习步骤提示 数据库事务 如何可以知道某个窗口被关闭了? 求助:Excel文档为某块区域设置表边框 问个弱智的问题! 求一个查询 c# web页面 如何打印服务器端pdf文件 我要做一个播放器啊,支持各种swf和flv格式的flash播放器
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;
using System.DirectoryServices;namespace GetIPMac
{
//private System.ComponentModel.Container components = null;
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
private string macAddress,ipAddress; private bool canPing = false;
private string GetMacAddress() // 得到指定IP的MAC地址
{
Int32 ldest = 0;
try
{
ldest = inet_addr(ipAddress);
}
catch (Exception iperr)
{
MessageBox.Show(iperr.Message);
}
Int64 macinfo = new Int64();
Int32 len = 6;
try
{
int res = SendARP(ldest, 0, ref macinfo, ref len);
}
catch (Exception err)
{
// throw new Exception("在解析MAC地址过程发生了错误!");
MessageBox.Show(err.Message);
}
string originalMACAddress = macinfo.ToString("X4");
if (originalMACAddress != "0000" && originalMACAddress.Length == 12)
{ //合法MAC地址
string mac1, mac2, mac3, mac4, mac5, mac6;
mac1 = originalMACAddress.Substring(10, 2);
mac2 = originalMACAddress.Substring(8, 2);
mac3 = originalMACAddress.Substring(6, 2);
mac4 = originalMACAddress.Substring(4, 2);
mac5 = originalMACAddress.Substring(2, 2);
mac6 = originalMACAddress.Substring(0, 2);
macAddress = mac1 + "-" + mac2 + "-" + mac3 + "-" + mac4 + "-" + mac5 + "-" + mac6;
canPing = true;
}
else
{
macAddress = "无法探测到MAC地址";
canPing = false;
}
return macAddress;
}
private void tbar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
{
switch (e.Button.Name)
{
case "Btn_search":
int i = 0;
DirectoryEntry root = new DirectoryEntry("WinNT:");
DirectoryEntries domains = root.Children;
domains.SchemaFilter.Add("domain");
foreach (DirectoryEntry domain in domains)
{
this.g_list.Items.Add(domain.Name.ToString());
DirectoryEntries computers = domain.Children;
computers.SchemaFilter.Add("computer");
foreach (DirectoryEntry computer in computers)
{
this.Grid_view.Rows.Add();
this.Grid_view.Rows[i].Cells[0].Value = i + 1;
this.Grid_view.Rows[i].Cells[1].Value = computer.Name.ToString();
IPHostEntry iphe = null;
try
{
iphe = Dns.GetHostEntry(computer.Name.ToString());
this.Grid_view.Rows[i].Cells[2].Value = iphe.AddressList[0].ToString();
ipAddress = iphe.AddressList[0].ToString();
this.Grid_view.Rows[i].Cells[3].Value = GetMacAddress(); }
catch
{
continue;
}
this.Grid_view.Rows[i].Cells[4].Value = domain.Name.ToString();
i++;
}
}
break;
}
} private void button1_Click(object sender, EventArgs e)
{
int i = 0;
DirectoryEntry root = new DirectoryEntry("WinNT:");
DirectoryEntries domains = root.Children;
domains.SchemaFilter.Add("domain");
Application.DoEvents();
foreach (DirectoryEntry domain in domains)
{
this.g_list.Items.Add(domain.Name.ToString()); DirectoryEntries computers = domain.Children;
computers.SchemaFilter.Add("computer");
foreach (DirectoryEntry computer in computers)
{
this.Grid_view.Rows.Add();
this.Grid_view.Rows[i].Cells[0].Value = i + 1;
this.Grid_view.Rows[i].Cells[1].Value = computer.Name.ToString();
IPHostEntry iphe = null;
try
{
iphe = Dns.GetHostEntry(computer.Name.ToString());
this.Grid_view.Rows[i].Cells[2].Value = iphe.AddressList[0].ToString();
ipAddress = iphe.AddressList[0].ToString();
this.Grid_view.Rows[i].Cells[3].Value = GetMacAddress(); }
catch
{
continue;
}
this.Grid_view.Rows[i].Cells[4].Value = domain.Name.ToString();
i++;
} }
}
}
}
{
partial class MainForm
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Grid_view = new System.Windows.Forms.DataGridView();
this.button1 = new System.Windows.Forms.Button();
this.g_list = new System.Windows.Forms.ListBox();
this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.Grid_view)).BeginInit();
this.SuspendLayout();
//
// Grid_view
//
this.Grid_view.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.Grid_view.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column1,
this.Column2,
this.Column3,
this.Column4,
this.Column5});
this.Grid_view.Location = new System.Drawing.Point(139, 28);
this.Grid_view.Name = "Grid_view";
this.Grid_view.RowTemplate.Height = 23;
this.Grid_view.Size = new System.Drawing.Size(515, 266);
this.Grid_view.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(533, 300);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(59, 26);
this.button1.TabIndex = 1;
this.button1.Text = "搜索";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// g_list
//
this.g_list.FormattingEnabled = true;
this.g_list.ItemHeight = 12;
this.g_list.Location = new System.Drawing.Point(13, 28);
this.g_list.Name = "g_list";
this.g_list.Size = new System.Drawing.Size(120, 268);
this.g_list.TabIndex = 2;
//
// Column1
//
this.Column1.HeaderText = "编号";
this.Column1.Name = "Column1";
//
// Column2
//
this.Column2.HeaderText = "机器名";
this.Column2.Name = "Column2";
//
// Column3
//
this.Column3.HeaderText = "IP地址";
this.Column3.Name = "Column3";
//
// Column4
//
this.Column4.HeaderText = "Mac地址";
this.Column4.Name = "Column4";
//
// Column5
//
this.Column5.HeaderText = "工作组";
this.Column5.Name = "Column5";
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(682, 338);
this.Controls.Add(this.g_list);
this.Controls.Add(this.button1);
this.Controls.Add(this.Grid_view);
this.Name = "MainForm";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.Grid_view)).EndInit();
this.ResumeLayout(false); } #endregion private System.Windows.Forms.DataGridView Grid_view;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox g_list;
private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
private System.Windows.Forms.DataGridViewTextBoxColumn Column4;
private System.Windows.Forms.DataGridViewTextBoxColumn Column5; }
}以是是获取局域网跟你一个工作组的计算机信息的
ComputerName 获取本地计算机的 NetBIOS 计算机名称。
{
//获取本机IP
string[] IP = new string[C_Common.GetLocalMachineInfo("IP").Length];
IP = C_Common.GetLocalMachineInfo("IP");
for (int i = 0; i < IP.Length; i++)
{
BRLibrary.Parameter.strLoaclIP = BRLibrary.Parameter.strLoaclIP + IP[i].ToString();
} //获取本机MAC
string[] Mac = new string[C_Common.GetLocalMachineInfo("MAC").Length];
Mac = C_Common.GetLocalMachineInfo("MAC");
for (int j = 0; j < Mac.Length; j++)
{
BRLibrary.Parameter.strLocalMAC = BRLibrary.Parameter.strLocalMAC + Mac[j];
}
if (string.IsNullOrEmpty(BRLibrary.Parameter.strLocalMAC) == false)
{
BRLibrary.Parameter.strLocalMAC = BRLibrary.Parameter.strLocalMAC.Substring(0, 12);
} //获取本机用户
string[] User = new string[C_Common.GetLocalMachineInfo("USER").Length];
User = C_Common.GetLocalMachineInfo("USER");
for (int k = 0; k < User.Length; k++)
{
BRLibrary.Parameter.strLocalUser = BRLibrary.Parameter.strLocalUser + User[k];
} }public string[] GetLocalMachineInfo(string Type)
{
switch (Type.ToUpper())
{
case "IP":
IPHostEntry IPHE = new IPHostEntry();
IPHE = Dns.GetHostEntry(Dns.GetHostName());
string[] resultIP = new string[IPHE.AddressList.Length];
for (int i = 0; i < IPHE.AddressList.Length; i++)
{
resultIP[i] = IPHE.AddressList[i].ToString();
}
return resultIP;
break;
case "MAC":
ManagementClass mc;
ManagementObjectCollection moc;
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
moc = mc.GetInstances();
int j = 0;
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
j = j + 1;
}
}
string[] resultMac = new string[j];
j = 0;
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
resultMac[j] = mo["MacAddress"].ToString().Replace(":", "");
j = j + 1;
}
}
return resultMac;
break;
case "HDD":
ManagementObject disk;
disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
disk.Get();
string[] resultHDD = new string[1];
resultHDD[0] = disk.GetPropertyValue("VolumeSerialNumber").ToString();
return resultHDD;
break;
case "USER":
ManagementClass mcUser = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection mocUser = mcUser.GetInstances();
int k = 0;
foreach (ManagementObject mo in mocUser)
{
k = k + 1;
}
string[] resultUser = new string[k];
k = 0;
foreach (ManagementObject mo in mocUser)
{
resultUser[k] = mo["UserName"].ToString(); k = k + 1;
}
return resultUser;
break;
default:
string[] resultNull = new string[1];
return resultNull;
break;
}
}
这个就可以