这篇文章介绍了C#获取应用程序路径或Web页面目录路径的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
一、Winform获取本程序的路径
1、获取当前目录
返回最后不带“\”的目录:如D:\Winform\bin\Debug
System.Windows.Forms.Application.StartupPath;
System.Environment.CurrentDirectory;
System.IO.Directory.GetCurrentDirectory();
返回最后带“\”的目录(AppDomain应用程序域):如D:\Winform\bin\Debug\
System.AppDomain.CurrentDomain.BaseDirectory;
System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
2、获取当前文件路径
System.Windows.Forms.Application.ExecutablePath;
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
System.Reflection.Assembly.GetExecutingAssembly().CodeBase; //或者
System.Reflection.Assembly.GetAssembly(typeof(类名)).CodeBase; //利用反射获取当前程序集的位置
typeof(类名).Assembly.Location;//利用反射
二、WebForm获取文件路径
虚拟目录名:WebSite1
指向:E:\mis\tools
本网页:http://localhost/WebSite1/folder/WebForm1.aspx
1、获取虚拟目录
根相对路径:
System.Web.HttpRuntime.AppDomainAppVirtualPath;
Request.ApplicationPath;
根绝对路径:
AppDomain.CurrentDomain.BaseDirectory;
Request.PhsicalApplicaitonPath;
Server.MapPath(“~”) \\ Server.MapPath("/WebSite1")
2、获取文件路径
当前文件相对路径、绝对路径
Request.Path //相对路径 /WebSite1/folder/WebForm1.aspx
Request.PhsicalPath //绝对路径 E:\mis\tools\folder\WebForm1.aspx
Request.AppRelativeCurrentExecutionFilePath //~/folder/WebForm1.aspx
当前目录
Server.MapPath(”.”)或Server.MapPath(””); //E:\mis\tools\folder
Server.MapPath(”./1.jpg”)或Server.MapPath(”1.jpg”); //E:\mis\tools\folder\1.jpg
上一目录
Server.MapPath(”..”) // E:\mis\tools
Server.MapPath(”../1.jpg”) //(””); //E:\mis\tools\1.jpg 上一目录下的1.JPG文件
Server.MapPath(”../..”) //C:\inputpub\wwwroot 上一目录的上一目录,到了顶目录wwwroot
根目录
Server.MapPath(”/”) //C:\inputpub\wwwroot
note:在HTML文件中,用”./”、”../”、”/”表示相对路径和绝对路径。
到此这篇关于C#获取文件路径的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持得得之家。
织梦狗教程
本文标题为:C#获取应用程序路径或Web页面目录路径


基础教程推荐
猜你喜欢
- Unity shader实现高斯模糊效果 2023-01-16
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#中 Json 序列化去掉null值的方法 2022-11-18
- C# 解析XML和反序列化的示例 2023-04-14
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- C#中的Linq to JSON操作详解 2023-06-08
- 实例详解C#实现http不同方法的请求 2022-12-26