//using System.Diagnostics;
//using System.IO;
//using System.Net.NetworkInformation;
//using System.Net.Sockets;
#region 获得本机MAC地址
/// <summary>
/// 通过DOS命令获得MAC地址
/// </summary>
/// <returns></returns>
public string GetMacAddressByDos()
{
string macAddress = "";
Process p = null;
StreamReader reader = null;
try
{
ProcessStartInfo start = new ProcessStartInfo("cmd.exe");
start.FileName = "ipconfig";
start.Arguments = "/all";
start.CreateNoWindow = true;
start.RedirectStandardOutput = true;
start.RedirectStandardInput = true;
start.UseShellExecute = false;
p = Process.Start(start);
reader = p.StandardOutput;
string line = reader.ReadLine();
while (!reader.EndOfStream)
{
if (line.ToLower().IndexOf("physical address") > 0 || line.ToLower().IndexOf("物理地址") > 0)
{
int index = line.IndexOf(":");
index += 2;
macAddress = macAddress + line.Substring(index) + ",";
}
line = reader.ReadLine();
}
}
catch
{
}
finally
{
if (p != null)
{
p.WaitForExit();
p.Close();
}
if (reader != null)
{
reader.Close();
}
}
return macAddress.Equals("") ? macAddress : macAddress.Substring(0, macAddress.Length - 1);
}
/// <summary>
/// 通过网络适配器获取MAC地址
/// </summary>
/// <returns></returns>
public string GetMacAddressByNetworkInformation()
{
string macAddress = "";
string tempMacAddress = "";
try
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
{
if (!adapter.GetPhysicalAddress().ToString().Equals(""))
{
tempMacAddress = adapter.GetPhysicalAddress().ToString();
for (int i = 1; i < 6; i++)
{
tempMacAddress = tempMacAddress.Insert(3 * i - 1, "-");
}
macAddress = macAddress + tempMacAddress + ",";
}
}
}
catch
{
}
return macAddress.Equals("") ? macAddress : macAddress.Substring(0, macAddress.Length - 1);
}
#endregion
#region 获得本机IPV4地址
/// <summary>
/// 通过DOS命令获得IP地址
/// </summary>
/// <returns></returns>
public string GetIPAddressByDos()
{
string ipAddress = "";
Process p = null;
StreamReader reader = null;
try
{
ProcessStartInfo start = new ProcessStartInfo("cmd.exe");
start.FileName = "ipconfig";
start.Arguments = "/all";
start.CreateNoWindow = true;
start.RedirectStandardOutput = true;
start.RedirectStandardInput = true;
start.UseShellExecute = false;
p = Process.Start(start);
reader = p.StandardOutput;
string line = reader.ReadLine();
while (!reader.EndOfStream)
{
if (line.ToLower().IndexOf("ip address") > 0 || line.ToLower().IndexOf("IP地址") > 0)
{
int index = line.IndexOf(":");
index += 2;
ipAddress = ipAddress + line.Substring(index) + ",";
}
line = reader.ReadLine();
}
}
catch
{
}
finally
{
if (p != null)
{
p.WaitForExit();
p.Close();
}
if (reader != null)
{
reader.Close();
}
}
return ipAddress.Equals("") ? ipAddress : ipAddress.Substring(0, ipAddress.Length - 1);
}
/// <summary>
/// 通过网络适配器获取IP地址
/// </summary>
/// <returns></returns>
public string GetIPAddressByNetworkInformation()
{
string ipAddress = "";
NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface NetworkIntf in NetworkInterfaces)
{
IPInterfaceProperties IPInterfaceProperties = NetworkIntf.GetIPProperties();
UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;
foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
{
if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
{
//排除本机自由IP
if (!UnicastIPAddressInformation.Address.ToString().Equals("127.0.0.1"))
ipAddress = ipAddress + UnicastIPAddressInformation.Address + ",";
}
}
}
return ipAddress.Equals("") ? ipAddress : ipAddress.Substring(0, ipAddress.Length - 1);
}
#endregion