예제 #1
0
    /// <summary>
    /// 获取IP对应机器名,返回主机名
    /// </summary>
    /// <param name="Pip"></param>
    /// <returns></returns>
    private string GetHostNameMy(string aPip)
    {
        string msg = "否";//表示不自动获取主机名

        if (!string.IsNullOrEmpty(aPip))
        {
            LearnSite.BLL.Computers   cbll   = new LearnSite.BLL.Computers();
            LearnSite.Model.Computers cmodel = new LearnSite.Model.Computers();
            cmodel = cbll.GetModelByIp(aPip);

            bool autohostname = LearnSite.Common.XmlHelp.GetAutoHostName(); //自动取主机名开关
            if (cmodel != null)                                             //如果存在
            {
                msg = cmodel.Pmachine;                                      //获取主机名
                if (!cmodel.Plock && autohostname)                          //如果未锁定,更新主机名并锁定
                {
                    string newMachine = LearnSite.Common.Computer.GetGuestHost(aPip);
                    cbll.UpdateByPid(cmodel.Pid, newMachine);
                    msg = newMachine;//返回新主机名
                }
            }
            else
            {
                if (autohostname)
                {
                    LearnSite.Model.Computers newmodel = new LearnSite.Model.Computers();//会丢失?不能过早定义?
                    newmodel.Pip   = aPip;
                    newmodel.Plock = true;
                    string addMachine = LearnSite.Common.Computer.GetGuestHost(aPip);
                    newmodel.Pmachine = addMachine;
                    newmodel.Pdate    = DateTime.Now;
                    cbll.Add(newmodel);
                    msg = addMachine;
                }
            }
        }
        else
        {
            msg = "空";//表示获取不到IP,反回主机名为空
        }
        return(msg);
    }