zoukankan      html  css  js  c++  java
  • C#(99):获取应用程序 或Web页面目录的路径

    一、Winform获取本程序的路径

    1、获取当前目录

    返回最后不带“”的目录:如D:WinforminDebug

    1. System.Windows.Forms.Application.StartupPath;
    2. System.Environment.CurrentDirectory;
    3. System.IO.Directory.GetCurrentDirectory();

    返回最后带“”的目录(AppDomain应用程序域):如D:WinforminDebug

    1. System.AppDomain.CurrentDomain.BaseDirectory;
    2. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

    2、获取当前文件路径

    1. System.Windows.Forms.Application.ExecutablePath;
    2. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
    3. System.Reflection.Assembly.GetExecutingAssembly().CodeBase; //或者System.Reflection.Assembly.GetAssembly(typeof(类名)).CodeBase; 利用反射获取当前程序集的位置
    4. typeof(类名).Assembly.Location;//利用反射

    二、WebForm获取文件路径

    虚拟目录名:WebSite1

    指向:E:mis ools

    本网页:http://localhost/WebSite1/folder/WebForm1.aspx

    1、获取虚拟目录

    根相对路径:

    1. System.Web.HttpRuntime.AppDomainAppVirtualPath;
    2. Request.ApplicationPath ;

    根绝对路径:

    1. AppDomain.CurrentDomain.BaseDirectory;
    2. Request.PhsicalApplicaitonPath; 
    3. Server.MapPath(“~”) \ Server.MapPath("/WebSite1")

    2、获取文件路径

    当前文件相对路径、绝对路径

    1. Request.Path      --相对路径 /WebSite1/folder/WebForm1.aspx
    2. Request.PhsicalPath      --绝对路径 E:mis oolsfolderWebForm1.aspx
    3. Request.AppRelativeCurrentExecutionFilePath      -- ~/folder/WebForm1.aspx

    当前目录

    1. Server.MapPath(”.”)或Server.MapPath(””);      --E:mis oolsfolder
    2. Server.MapPath(”./1.jpg”)或Server.MapPath(”1.jpg”);     --E:mis oolsfolder1.jpg

    上一目录

    1. Server.MapPath(”..”)     -- E:mis ools
    2. Server.MapPath(”../1.jpg”) --(””);     --E:mis ools1.jpg 上一目录下的1.JPG文件
    3. Server.MapPath(”../..”)     --C:inputpubwwwroot 上一目录的上一目录,到了顶目录wwwroot

    根目录

    1. Server.MapPath(”/”) --–C:inputpubwwwroot

    note:在HTML文件中,用”./”、”../”、”/”表示相对路径和绝对路径。

  • 相关阅读:
    设计模式_桥梁模式
    C++基础学习教程(一)
    Python图像处理(11):k均值
    Cygwin-安装和配置ssh服务
    LeetCode11:Container With Most Water
    事务四大特征:原子性,一致性,隔离性和持久性(ACID)
    2014微软编程之美初赛第一场第三题 活动中心
    改进xutils下载管理器,使其,在随意地方进行进度更新,以及其它状态监听操作
    IT痴汉的工作现状21-Android开发前景论
    Spring配置文件总结
  • 原文地址:https://www.cnblogs.com/springsnow/p/9434002.html
Copyright © 2011-2022 走看看