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

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
宝玉的分享
宝玉的分享
爱范儿
爱范儿
Last Week in AI
Last Week in AI
U
Unit 42
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
D
DataBreaches.Net
Recent Commits to openclaw:main
Recent Commits to openclaw:main
雷峰网
雷峰网
T
The Exploit Database - CXSecurity.com
L
LangChain Blog
C
CERT Recently Published Vulnerability Notes
S
Schneier on Security
C
Cisco Blogs
MongoDB | Blog
MongoDB | Blog
G
GRAHAM CLULEY
Hacker News - Newest:
Hacker News - Newest: "LLM"
大猫的无限游戏
大猫的无限游戏
L
LINUX DO - 最新话题
D
Docker
K
Kaspersky official blog
Security Latest
Security Latest
博客园 - 【当耐特】
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Hacker News
The Hacker News
P
Privacy International News Feed
Microsoft Security Blog
Microsoft Security Blog
V2EX - 技术
V2EX - 技术
The Last Watchdog
The Last Watchdog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Martin Fowler
Martin Fowler
Latest news
Latest news
Project Zero
Project Zero
TaoSecurity Blog
TaoSecurity Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threat Research - Cisco Blogs
H
Heimdal Security Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Help Net Security
Help Net Security
A
Arctic Wolf
Cisco Talos Blog
Cisco Talos Blog
Engineering at Meta
Engineering at Meta
M
MIT News - Artificial intelligence

博客园 - 文强

sql优化的几种方式 UpdatePanel 无刷新弹出窗口 .net web 点击链接在页面指定位置显示DIV的问题 重建主键 sql 日期时间格式转换 UpdatePanel无法直接弹出窗口的解决 A标签执行JS脚本 Guid 的几种形式 iis 5.1 连接 sql 2005 CSS 垂直居中 js 获取服务器控件 js CSS VS 打包升成可自动升级的安装包 js 弹出页面传值 提示消息后跳转 spoolsv.exe 报错,无法打印 C# web 网页刷新时数据集的保存和应用 在网页中插入CSS样式表的几种方法
C#获取文件的当前路径
文强 · 2013-07-03 · via 博客园 - 文强

C#获取文件的当前路径

1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName

-获取模块的完整路径。

2.System.Environment.CurrentDirectory

-获取和设置当前目录(该进程从中启动的目录)的完全限定目录。

3.System.IO.Directory.GetCurrentDirectory()

-获取应用程序的当前工作目录。这个不一定是程序从中启动的目录啊,

4.System.AppDomain.CurrentDomain.BaseDirectory

-获取程序的基目录。

5.System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase

-获取和设置包括该应用程序的目录的名称。

6. System.Windows.Forms.Application.StartupPath

-获取启动了应用程序的可执行文件的路径。效果和2、5一样。只是5返回的字符串后面多了一个"\"而已

7.System.Windows.Forms.Application.ExecutablePath

-获取启动了应用程序的可执行文件的路径及文件名,效果和1一样。

对于Windows程序 和Web 应用程序来说,他们运行的路径是不一样的,所以关键是判断当前运行的程序是哪种程序.于是我们可以使用如下的代码

string path = "";

if (System.Environment.CurrentDirectory ==AppDomain.CurrentDomain.BaseDirectory)//Windows应用程序则相等

...{

path = AppDomain.CurrentDomain.BaseDirectory;

}

else

...{

path = AppDomain.CurrentDomain.BaseDirectory + "Bin\";

}

这样如果我们写了一个类库,类库中用到了Assembly.LoadFrom,由于是通用类库,所以可能用到Windows程序中也可能用到Web中,那么用上面的代码就很方便了.

1、Server.MapPath

2、System.Windows.Forms.StartupPath

3、Type.Assembly.Location

方法2可以应用于控制台应用程序,WinForm应用程序,Windows服务,方法1可以应用于Web应用程序,方法3都可以应用。

但方法3是加载应用程序的路径。如果是Web应用程序,取得的路径是:C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files目录。所以Web项目还是使用Server.MapPath吧。否则建议使用方法2。如果自己新建类库。可以加入对System.Windows.Forms.StartupPath的引用后使用