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文件中,用”./”、”../”、”/”表示相对路径和绝对路径。

  • 相关阅读:
    jquery的promise和es6的promise的区别
    vue源码
    Paging through lots of data efficiently (and in an Ajax way) with ASP.NET 2.0
    laydate 1.1 下拉日期挡住的bug修复
    Microsoft SQL Server 2008 R2 官方简体中文正式版下载(附激活序列号密钥)
    使用ASP.NET Treeview显示数据库里DataTable里的数据。
    [C# .NET] 数组排序 Arrays
    字符串数据按照大小排序
    Chrome浏览器中的三种刷新模式
    『TensorFlow』lookup.index_table_from_tensor查找表映射函数
  • 原文地址:https://www.cnblogs.com/springsnow/p/9434002.html
Copyright © 2011-2022 走看看