C# 获取当前执行DLL 所在路径
有的时候,当前执行的DLL 和启动的EXE 所在路径并不一致,这时我们想要获得当前执行DLL 所在路径可以使用下面的方法。
// Summary:
// Gets the path or UNC location of the loaded file that contains the manifest.
//
// Returns:
// The location of the loaded file that contains the manifest. If the loaded file
// was shadow-copied, the location is that of the file after being shadow-copied.
// If the assembly is loaded from a byte array, such as when using the System.Reflection.Assembly.Load(System.Byte[])
// method overload, the value returned is an empty string ("").
//
// Exceptions:
// T:System.NotSupportedException:
// The current assembly is a dynamic assembly, represented by an System.Reflection.Emit.AssemblyBuilder
// object.
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
// Summary:
// Gets the location of the assembly as specified originally, for example, in an
// System.Reflection.AssemblyName object.
//
// Returns:
// The location of the assembly as specified originally.
public static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
通过 CodeBase 得到一个 URI 格式的路径;
用 UriBuild.UnescapeDataString 去掉前缀 File://;
用 GetDirectoryName 把它变成正常的 windows 格式。
C# 获取当前执行DLL 所在路径的更多相关文章
- c#获取当前应用程序所在路径
一.获取当前文件的路径1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName 获取模块的完整路径,包括文 ...
- java获取当前执行文件的路径
需要知道执行jar包时,jar包所在的路径. 开始使用了 p.getClass().getResource("/").getPath(); 结果在IDE里面使用是好的,但是在命令行 ...
- C#: 获取当前应用程序所在路径
ref: http://www.cnblogs.com/netlyf/archive/2011/06/22/2086718.html 一.获取当前文件的路径 string str1=Process.G ...
- C#.net 获取当前应用程序所在路径及环境变量
一.获取当前文件的路径 string str1=Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名. string st ...
- 【C#学习笔记】获取当前应用程序所在路径及环境变量
转自:http://www.cnblogs.com/netlyf/archive/2011/06/22/2086718.html 一.获取当前文件的路径 string str1=Process.Get ...
- C#获取当前应用程序所在路径及环境变量
一.获取当前文件的路径 string str1=Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名. string st ...
- (转载)C#获取当前应用程序所在路径及环境变量
一.获取当前文件的路径 string str1=Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名. string st ...
- Shell 获取当前执行脚本的路径
filepath=$(cd "$(dirname "$0")"; pwd) 脚本文件的绝对路径存在了环境变量filepath中,可以用 echo $filepa ...
- 关于DLL搜索路径顺序的一个问题
DLL的动态链接有两种方法.一种是加载时动态链接(Load_time dynamic linking).Windows搜索要装入的DLL时,按以下顺序:应用程序所在目录→当前目录→Windows SY ...
随机推荐
- 计算机组成原理 — GPU 图形处理器
目录 文章目录 目录 显卡 GPU GPU 与深度学习 GPU 与 CPU 体系结构的区别 GPU 显存与 CPU 主存的区别 GPU 与 CPU 之间的数据交互方式 GPU 的体系结构 GPU 的工 ...
- delphi 需要应用一个单元是,需要在工程里面先添加单元
delphi 需要应用一个单元是,需要在工程里面先添加单元
- R语言常用包简介
- 20190903 - CSDN 的奇葩替换
可能是出于安全原因 CSDN 对内容中的代码,作了很多奇葩的替换. 比如下面两行,是否有差别? # - # -16 有.其实 cut 后的短横线,内部编码不同,前者复制后无法被识别. 再比如下面两个词 ...
- 算法题--Z字形变换
题目描述 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L C I ...
- 深入理解C语言-结构体做函数参数
结构体做函数参数,在C语言中属于常见现象,此时为了内存考虑,不传递结构体,而是传递结构体的地址 结构体定义 struct Man { char name[64]; int age; }; 结构体可以与 ...
- git stash save -a 遇到的坑 , 弹出匿藏错误
情景一: 用命令行的 : git stash save -u "描述" git stash save -a "描述" -u: 会把没有记录到的文件也保存下来(比 ...
- Redis二进制反转算法分析
在 redis 源码中 dictScan 算法中用到了用到了非常经典的二进制反转算法,该算法对二进制的反转高效而实用,同时对于理解位运算也有非常大的帮助.先呈现源码: /* Function to r ...
- 腾讯云服务器的CPU ---发现大厂都很那啥
今天给同事看了下 腾讯云上面的ora从了数据库的启动问题 简单看了下 硬件配置如下: System: Host: VM_0_8_centos Kernel: -.el6.x86_64 x86_64 b ...
- SQL用法三(游标和Fetch)
/一般情况下,我们用SELECT这些查询语句时,都是针对的一行记录而言,如果要在查询分析器中对多行记录(即记录集)进行读取操作时,则需要使用到游标或WHILE等循环/以下内容摘自http://www. ...