Dzwebs.Net

撰写电脑技术杂文十余年

在C#环境下获取域名和物理目录地址的代码

Admin | 2013-1-6 10:52:47 | 被阅次数 | 9660

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  通过C#,可获取域名及网站的物理地址,代码分别如下,首先要添加好引用。

  一般情况下,我们是使用如下简短的代码获取的:

  string strHostName = DNS.GetHostName();//得到本主机名

  IPHostEntry ipEntry = DNS.GetHostByName(strHostName);//取得本机IP

  string strAddr = ipEntry.AddressList[0].ToString();//假设本地主机为单网卡

  这段代码中使用了两个类,一个是DNS类,另一个是IPHostEntry类,二者都存在于命名空间System.Net中。

  但是,要获取更完整的更复杂的,就得靠如下代码了。

  using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Web;

  namespace SinvanCMS.Common
  {
   
     public class Application
     {
        //如下代码是获取URL
        public static string AppUrl
        {
            get
            {
                return "http://" + System.Web.HttpContext.Current.Request.Url.Host.ToString() + AppSiteName;
            }
        }

       
        public static string AppSiteName
        {
            get
            {
                string SiteAddress = "";
                SiteAddress = System.Web.HttpContext.Current.Request.ApplicationPath.ToString();
                if (System.Web.HttpContext.Current.Request.ApplicationPath.ToString() == "/")
                {
                    SiteAddress = "";
                }
                else
                {
                    SiteAddress = System.Web.HttpContext.Current.Request.ApplicationPath.ToString();
                }
                return SiteAddress.ToString();
            }
        }

        //如下代码是获取物理地址
        public static string AppMapPath
        {
            get
            {
                string ApplicationPath = System.Web.HttpContext.Current.Server.MapPath("~/");
                if (ApplicationPath.EndsWith("//") == true)
                {
                    ApplicationPath = ApplicationPath.Remove(ApplicationPath.Length - 1);
                }
                return ApplicationPath;
              }
          }
      }
  }


该杂文来自: 网站开发杂文

上一篇:C#获取不同路径的代码

下一篇:c#如何确定弹出窗体的位置

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计