zoukankan      html  css  js  c++  java
  • Asp.net 获取网站根目录

    获取网站根目录的方法有几种如:

    Server.MapPath(Request.ServerVariables["PATH_INFO"])
     Server.MapPath("/")
     Server.MapPath("")
    Server.MapPath(".")
     Server.MapPath("../")
     Server.MapPath("..")
           Page.Request.ApplicationPath

    以上的代码在http://localhost/EnglishClub/manage/WebForm1.aspx页面
    运行结果:
    C:\Inetpub\wwwroot\EnglishClub\manage\WebForm1.aspx
    C:\Inetpub\wwwroot\
    C:\Inetpub\wwwroot\EnglishClub\manage
    C:\Inetpub\wwwroot\EnglishClub\manage
    C:\Inetpub\wwwroot\EnglishClub\
    C:\Inetpub\wwwroot\EnglishClub

    以上的方法可以在.aspx中访问,但是如果你在。cs文件就不能用。

    HttpContext.Current.Server.MapPath();
    System.Web.HttpContext.Current.Request.PhysicalApplicationPath   

    在.cs文件中可以用。但是HttpContext.Current.Server.MapPath();这个获取的是文件的路径而不是根目录。只有

    System.Web.HttpContext.Current.Request.PhysicalApplicationPath   这个才是获取的根目录,在写获取数据库路径是应该用这个,其他的都有问题。 

     

    ----------------------------------------------------------------------------------

    ----------------------------------------------------------------------------------

     

     编写程序的时候,经常需要用的项目根目录,自己总结如下:

      1、取得控制台应用程序的根目录方法

      方法1、Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径

      方法2、AppDomain.CurrentDomain.BaseDirectory 获取基目录,它由程序集冲突解决程序用来探测程序集

      2、取得Web应用程序的根目录方法

      方法1、HttpRuntime.AppDomainAppPath.ToString();//获取承载在当前应用程序域中的应用程序的应用程序目录的物理驱动器路径。用于App_Data中获取

      方法2、Server.MapPath("") 或者 Server.MapPath("~/");//返回与Web服务器上的指定的虚拟路径相对的物理文件路径

      方法3、Request.ApplicationPath;//获取服务器上ASP.NET应用程序的虚拟应用程序根目录

      3、取得WinForm应用程序的根目录方法

      1、Environment.CurrentDirectory.ToString();//获取或设置当前工作目录的完全限定路径

      2、Application.StartupPath.ToString();//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称

      3、Directory.GetCurrentDirectory();//获取应用程序的当前工作目录

      4、AppDomain.CurrentDomain.BaseDirectory;//获取基目录,它由程序集冲突解决程序用来探测程序集

      5、AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取或设置包含该应用程序的目录的名称

      其中:以下两个方法可以获取执行文件名称

      1、Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名。

      2、Application.ExecutablePath;//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称

     

     

     

  • 相关阅读:
    Linux修改时间
    Oracle 审计文件
    system表空间
    Windows操作系统添加永久静态路由
    Linux添加永久静态路由
    如何把excel中的行转为列?
    Linux中测试网络命令
    Linux中查看网络命令
    2018.3.29 网页中嵌套网页的两种方法
    2018.3.29 div内容格式设置
  • 原文地址:https://www.cnblogs.com/nogodsend/p/2207104.html
Copyright © 2011-2022 走看看