• Asp.net 无网络情况下 MaC 地址异常的问题


    原始程序代码

    代码
    /// <summary>
    /// 本地的Mac地址
    /// </summary>
    public static string LocalMacIP
    {
    get
    {
    ManagementClass mc;
    ManagementObjectCollection moc;
    mc
    = new ManagementClass("Win32_NetworkAdapterConfiguration");
    moc
    = mc.GetInstances();
    string str = "";
    foreach (ManagementObject mo in moc)
    {
    if ((bool)mo["IPEnabled"] == true)
    str
    = mo["MacAddress"].ToString();

    }
    //return str;
    return "00:19:7E:CC:ED:B5";
    }

    }

    结果和在cmd下ipconfig的结果不同,仅是一个vm的mac

    代码
    string MacAddress;
    //设置MAC地址
    string MAC="";
    ManagementClass MC
    = new ManagementClass ( "Win32_NetworkAdapterConfiguration" );
    ManagementObjectCollection MOC
    = MC.GetInstances();
    foreach ( ManagementObject moc in MOC )
    {
    if ( moc["IPEnabled"].ToString() == "True")
    {
    MAC
    = moc["MacAddress"].ToString();
    }
    }
    MacAddress
    =MAC;

    代码
    using System.Management;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Text.RegularExpressions;

    string strsearch=string.Format(@"SELECT MACAddress FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL)AND (Manufacturer <> 'Microsoft'))");

    System.Management.ManagementObjectSearcher msearch
    =new ManagementObjectSearcher();
    msearch.Scope
    =new ManagementScope(@"root\CIMV2");
    msearch.Query
    =new ObjectQuery(strsearch);
    ArrayList MacList
    =new ArrayList();
    int i =0;
    try
    {
    if((msearch.Get().Count<1) || (msearch.Get()==null))
    {
    MessageBox.Show(
    "No MAC Address!");
    }
    else
    {
    MessageBox.Show(
    "MAC Address No. is : "+ msearch.Get().Count.ToString());
    foreach(System.Management.ManagementObject mymac in msearch.Get())
    {
    string str1=string.Format("MAC{0} address:{1}",i.ToString(),mymac["MACAddress"].ToString());
    MessageBox.Show(
    "MAC Address "+i.ToString()+":"+str1);
    MacList.Add(str1);
    i
    ++;
    }
    }
    }
    catch(Exception ex)
    {
    string msg=ex.Message.ToString();
    MessageBox.Show(
    "erro", msg);
    }

     获得本机mac的方法,只是结果是一个ArrayList,运行的结果和cmd下命令一样,只是包括了一个真的mac和两个vm的mac

    代码
    using System.Management;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Text.RegularExpressions;

    [DllImport(
    "Iphlpapi.dll")]
    private static extern int SendARP(Int32 dest,Int32 host,ref Int32 mac,ref Int32 length);
    [DllImport(
    "Ws2_32.dll")]
    private static extern Int32 inet_addr(string ip);

    string IPAddress=System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList[0].ToString();
    MessageBox.Show(
    "ip: "+IPAddress);
    string mac = this.GetMACFromIP(IPAddress);
    MessageBox.Show(
    "mac: "+mac);

    private string GetMACFromIP(string A_strIP)
    {
    string strRet = "Unknown";

    string strIPPattern = @"^\d+\.\d+\.\d+\.\d+$";

    Regex objRex
    =new Regex(strIPPattern);

    if(objRex.IsMatch(A_strIP) == true)
    {
    Int32 intDest
    = inet_addr(A_strIP);
    Int32 [] arrMAC
    = new Int32[2];
    Int32 intLen
    = 6;

    int intResult = SendARP(intDest,0,ref arrMAC[0],ref intLen);

    if(intResult == 0)
    {
    Byte [] arrbyte
    = new Byte[8];
    arrbyte[
    5] = (Byte)(arrMAC[1] >> 8);
    arrbyte[
    4] = (Byte)arrMAC[1];
    arrbyte[
    3] = (Byte)(arrMAC[0] >> 24);
    arrbyte[
    2] = (Byte)(arrMAC[0] >> 16);
    arrbyte[
    1] = (Byte)(arrMAC[0] >> 8);
    arrbyte[
    0] = (Byte)arrMAC[0];

    StringBuilder strbMAC
    = new StringBuilder();

    for(int intIndex = 0 ;intIndex < 6;intIndex ++)
    {
    if(intIndex > 0) strbMAC.Append("-");
    strbMAC.Append(arrbyte[intIndex].ToString(
    "X2"));
    }
    strRet
    = strbMAC.ToString();
    }
    }

    return strRet;
    }

    正常..

  • 相关阅读:
    BZOJ 2565 最长双回文串 (Manacher)
    BZOJ 3881 [COCI2015]Divljak (Trie图+Fail树+树链的并+树状数组维护dfs序)
    BZOJ 3530 [SDOI2014]数数 (Trie图/AC自动机+数位DP)
    BZOJ 1444 [JSOI2009]有趣的游戏 (Trie图/AC自动机+矩阵求逆)
    BZOJ 1195 [HNOI2006]最短母串 (Trie图+状压+bfs最短路)
    luogu P5289 [十二省联考2019]皮配
    luogu P5285 [十二省联考2019]骗分过样例
    luogu P5294 [HNOI2019]序列
    luogu P5292 [HNOI2019]校园旅行
    luogu P5284 [十二省联考2019]字符串问题
  • 原文地址:https://www.cnblogs.com/pchgo/p/1689651.html
Copyright © 2020-2023  润新知