我想从C#中的iOS mobile.config
文件中获取MAC地址。
谁能告诉我怎么样?
答案 0 :(得分:1)
在这里,我们可以使用ASP.Net C#代码找到iOS设备的MAC地址:
var userAgent = HttpContext.Current.Request.UserAgent.ToLower(); // User's Iphone/Ipad Info.
var userAgent1 = HttpContext.Current.Request.UserHostAddress; // User's Iphone/Ipad Mac Address
GetMacAddressfromIP macadd = new GetMacAddressfromIP();
if (userAgent.Contains("iphone;"))
{
// iPhone
Label1.Text = userAgent;
Label2.Text = userAgent1;
string Getmac = macadd.GetMacAddress(userAgent1);
Label3.Text = Getmac;
}
else if (userAgent.Contains("ipad;"))
{
// iPad
Label1.Text = userAgent;
Label2.Text = userAgent1;
string Getmac = macadd.GetMacAddress(userAgent1);
Label3.Text = Getmac;
}
else
{
Label1.Text = userAgent;
Label2.Text = userAgent1;
string Getmac = macadd.GetMacAddress(userAgent1);
Label3.Text = Getmac;
}
public string GetMacAddress(string ipAddress)
{
string macAddress = string.Empty;
if (!IsHostAccessible(ipAddress)) return null;
try
{
ProcessStartInfo processStartInfo = new ProcessStartInfo();
Process process = new Process();
processStartInfo.FileName = "arp";
processStartInfo.RedirectStandardInput = false;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.Arguments = "-a " + ipAddress;
processStartInfo.UseShellExecute = false;
process = Process.Start(processStartInfo);
int Counter = -1;
while (Counter <= -1)
{
Counter = macAddress.Trim().ToLower().IndexOf("mac address", 0);
if (Counter > -1)
{
break;
}
macAddress = process.StandardOutput.ReadLine();
if (macAddress != "")
{
string[] mac = macAddress.Split(' ');
if (Array.IndexOf(mac, ipAddress) > -1)
{
if (mac[11] != "")
{
macAddress = mac[11].ToString();
break;
}
}
}
}
process.WaitForExit();
macAddress = macAddress.Trim();
}
catch (Exception e)
{
Console.WriteLine("Failed because:" + e.ToString());
}
return macAddress;
}