Wie bereits im Java Forum gepostet zeig ich euch auch noch wie ihr in C# die Mac-Adresse rausfindet.
Beachtet, dass ihr hierfür die Library
Hier stelle ich euch ein kleines tool mit GUI vor welches ich entwickelt habe um die Mac-Adresse auszulesen:
DefaultGUI.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Collections;
namespace MacGrabber
{
public partial class DefaultGUI : Form
{
public DefaultGUI()
{
InitializeComponent();
getNetworkData();
}
List<NetworkItem> NetworkData = new List<NetworkItem>();
private void getNetworkData()
{
int i = 0;
try
{
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
if (mo["MacAddress"] != null && mo["IPAddress"] != null)
{
NetworkData.Add(new NetworkItem(i, mo["Description"].ToString(), mo["MacAddress"].ToString().Replace(":", "")));
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Source);
Console.WriteLine(ex.Message);
}
foreach (NetworkItem item in NetworkData)
comboBoxMacList.Items.Add(item);
if (this.comboBoxMacList.Items.Count > 0)
this.comboBoxMacList.SelectedIndex = 0;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBoxMacField_TextChanged(object sender, EventArgs e)
{
}
private void setClipboard(string cbtext)
{
Clipboard.SetText(cbtext);
}
private void comboBoxMacList_SelectedIndexChanged(object sender, EventArgs e)
{
NetworkItem selectedItem = (sender as ComboBox).SelectedItem as NetworkItem;
if (selectedItem != null)
{
settextBoxMacField(selectedItem.getMacAddr());
setClipboard(selectedItem.getMacAddr());
}
}
}
}
DefaultGUIDesigner.cs
namespace MacGrabber
{
partial class DefaultGUI
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DefaultGUI));
this.labelMac = new System.Windows.Forms.Label();
this.textBoxMacField = new System.Windows.Forms.TextBox();
this.comboBoxMacList = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// labelMac
//
this.labelMac.AutoSize = true;
this.labelMac.Location = new System.Drawing.Point(13, 7);
this.labelMac.Name = "labelMac";
this.labelMac.Size = new System.Drawing.Size(72, 13);
this.labelMac.TabIndex = 0;
this.labelMac.Text = "Mac-Adresse:";
this.labelMac.Click += new System.EventHandler(this.label1_Click);
//
// textBoxMacField
//
this.textBoxMacField.Location = new System.Drawing.Point(90, 4);
this.textBoxMacField.Name = "textBoxMacField";
this.textBoxMacField.Size = new System.Drawing.Size(208, 20);
this.textBoxMacField.TabIndex = 1;
this.textBoxMacField.TextChanged += new System.EventHandler(this.textBoxMacField_TextChanged);
//
// comboBoxMacList
//
this.comboBoxMacList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxMacList.Location = new System.Drawing.Point(16, 30);
this.comboBoxMacList.Name = "comboBoxMacList";
this.comboBoxMacList.Size = new System.Drawing.Size(282, 21);
this.comboBoxMacList.TabIndex = 2;
this.comboBoxMacList.SelectedIndexChanged += new System.EventHandler(this.comboBoxMacList_SelectedIndexChanged);
//
// DefaultGUI
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(310, 58);
this.Controls.Add(this.comboBoxMacList);
this.Controls.Add(this.textBoxMacField);
this.Controls.Add(this.labelMac);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "DefaultGUI";
this.Text = "Mac Grabber";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private void settextBoxMacField(string text) { this.textBoxMacField.Text = text; }
private void addComboBoxMacList(string item) { this.comboBoxMacList.Items.Add(item); }
private System.Windows.Forms.Label labelMac;
private System.Windows.Forms.TextBox textBoxMacField;
public System.Windows.Forms.ComboBox comboBoxMacList;
}
}
NetworkItem.cs
using System;
using System.Management;
using System.Windows.Forms;
using System.Collections;
public class NetworkItem
{
public NetworkItem(int id, String desc, String mac)
{
this.id = id;
this.description = desc;
this.macaddr = mac;
}
public String getMacAddr()
{
return this.macaddr;
}
public String getDescription()
{
return this.description;
}
public int getID()
{
return this.id;
}
public override string ToString()
{
return this.description;
}
private String macaddr;
private String description;
private int id;
}
Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace MacGrabber
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run( new DefaultGUI() );
}
}
}
Auf wunsch publiziere ich gerne auch den Sourcecode in einem VS Projekt, bzw. die Binary in einem Archiv.
2 Antworten für "Mac Adresse rausfinden in C#"
Der Beitrag ist zwar schon älter, aber genau danach habe ich gesucht!
Hallo Miteinander,
ich bin froh daß ich Eusch gefunden habe, es sind Hilfreiche Tipps und Tricks. Bussi Birgit
Kommentar verfassen