-1

我想从mobile.configC# 中的 iOS 文件中获取 MAC 地址。

谁能告诉我怎么做?

4

1 回答 1

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;

}
于 2013-04-23T07:30:32.763 回答