惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - zhaowt001

HTML 转 PDF Sort using in VS 炒股经验 完美解读Linux中文件系统的目录结构 30个优秀.net在线学习资源站点 如何删除windows service(转帖) Windows CE 电源管理(转贴) C#中获取程序当前路径的集中方法 随心所欲操作Enum枚举类型 (转贴) 远程连接SQL Server 2000服务器的解决方案 - zhaowt001 对软件的新认识 EVENT IIS 配置 - zhaowt001 - 博客园 一个程序员成长的六个阶段 优秀程序员应当具备的品质 - zhaowt001 ASP.NET的网站的设计与优化 - zhaowt001 override new 关键字的区别 解决IIS服务器Web访问提示输入密码 - zhaowt001 山东人!
C#、ASP.NET获取当前应用程序的绝对路径,获取程序工作路径 (转帖)
zhaowt001 · 2014-07-17 · via 博客园 - zhaowt001

进程对象在.NET中表现为System.Diagnostics.Process类,通过调用Process.GetCurrentProcess().MainModule.FileName可获得当前执行的exe的文件名。但是这个方法得到的仅仅是文件名,如果程序运行期间没有切换工作目录,那么可以调用System.IO.Path的方法获取绝对路径。但是当前目录同样可以通过Environment.CurrentDirectory获得,而且很多软件在使用Open Dialog打开文件的时候,都会切换工作目录,而使得这一机制失效。

如果是在Windows Forms应用程序中,当前应用也表现为System.Windows.Forms.Application对象,通过其静态属性Application.ExecutablePath和Application.StartupPath,可以取得可执行文件的路径和启动路径。

但如果不是在Windows应用中呢,或者是在Library中呢,就算是Application对象的属性依然能获得,也需要在工程中添加System.Windows.Forms这个Assembly的引用,非常不方便。这个时候,可以通过Assembly的静态方法,GetCallingAssembly或者GetExecutingAssembly取得当前执行的Assembly,然后通过Assembly类的Location获取assembly的位置。

但是使用Assembly的时候,可能会遇到权限方面的问题,同时Assembly.GetCallingAssembly或者Assembly.GetExecutingAssembly有可能得到的不是.exe文件的位置。在GAC中的添加了强名的Assembly,运行时是可以不必与.exe在同一目录的。

.NET的进程启动时,会创建AppDomain,所有的Assembly都被Load到某一个AppDomain中,而AppDomain中提供了SetupInformation属性,可以获取AppDomain启动时的一些信息,因此,可以通过调用AppDomain.CurrentDomain.SetupInformation.ApplicationBase获取当前应用程序所在的路径。

在通过以上方法取到所需的目录后,可以调用System.IO.Path的方法获取文件名,目录名,绝对路径等。停止对路径字符串的分析,而改用System.IO.Path类吧。

开发.NET下应用程序时,了解Process/Application->AppDomain->Assembly的关系,对于实现正确的逻辑,是非常有帮助的。

// 获取程序工作路径的 n 种方法。
string tmp = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
tmp = System.Environment.CurrentDirectory;
tmp = System.IO.Directory.GetCurrentDirectory();
tmp = System.Windows.Forms.Application.StartupPath;
tmp = System.Windows.Forms.Application.ExecutablePath;
//针对网站应用程序有效的 m 种方法。
tmp = System.AppDomain.CurrentDomain.BaseDirectory;
tmp = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
tmp = HttpContext.Current.Request.PhysicalApplicationPath;

AppDomain.CurrentDomain.SetupInformation.ApplicationBase此方法可使用在ASP.NET中取代Page.Server.MapPath(),可以说这个获取路径的方法是.NET通用的获取路径方法,建议使用。