有的时候,当前执行的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 所在路径的更多相关文章

  1. c#获取当前应用程序所在路径

    一.获取当前文件的路径1.   System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName     获取模块的完整路径,包括文 ...

  2. java获取当前执行文件的路径

    需要知道执行jar包时,jar包所在的路径. 开始使用了 p.getClass().getResource("/").getPath(); 结果在IDE里面使用是好的,但是在命令行 ...

  3. C#: 获取当前应用程序所在路径

    ref: http://www.cnblogs.com/netlyf/archive/2011/06/22/2086718.html 一.获取当前文件的路径 string str1=Process.G ...

  4. C#.net 获取当前应用程序所在路径及环境变量

    一.获取当前文件的路径 string str1=Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名. string st ...

  5. 【C#学习笔记】获取当前应用程序所在路径及环境变量

    转自:http://www.cnblogs.com/netlyf/archive/2011/06/22/2086718.html 一.获取当前文件的路径 string str1=Process.Get ...

  6. C#获取当前应用程序所在路径及环境变量

    一.获取当前文件的路径 string str1=Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名. string st ...

  7. (转载)C#获取当前应用程序所在路径及环境变量

    一.获取当前文件的路径 string str1=Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名. string st ...

  8. Shell 获取当前执行脚本的路径

    filepath=$(cd "$(dirname "$0")"; pwd) 脚本文件的绝对路径存在了环境变量filepath中,可以用 echo $filepa ...

  9. 关于DLL搜索路径顺序的一个问题

    DLL的动态链接有两种方法.一种是加载时动态链接(Load_time dynamic linking).Windows搜索要装入的DLL时,按以下顺序:应用程序所在目录→当前目录→Windows SY ...

随机推荐

  1. Can't initialize physical volume "/dev/sdb" of volume group "cinder-volumes" without -ff /dev/sdb: physical volume not initialized.

    原因:无法初始化物理量,之前创建的cinder-volumes没有卸载 方法一: [root@storage cinder]# lsblk NAME MAJ:MIN RM SIZE RO TYPE M ...

  2. 关于虚拟机可以ping通本机,本机可以ping通虚拟机,但是虚拟机就是不能上网

    1  需要确定虚拟的防火墙已经关闭 2 修改vi /etc/sysconfig/network-scripts/ifcfg-eth0配置文件  增加以下参数 BOOTPROTO=none------- ...

  3. 在TeamCity中执行gtest单元测试

    1. 在Visual Studio 2017中新建一个gtest项目 Sample-Test1.这个项目会自动安装“Microsoft.googletest.v140.windesktop.msvcs ...

  4. svn 创建外部链接

    背景: 之前的服务器项目<杀戮链条>,服务器工程中的common目录需要在多个子工程中共享,为了统一,所以只保留一个common目录,在子工程中采用外部链接的方法共享到工程中 1.  打开 ...

  5. JAVA语言课堂测试01源代码(学生成绩管理系统)

    package 考试; /*信1807-8 * 20183798 * 向瑜 */ import java.util.Scanner; //ScoreInformation 类 class ScoreI ...

  6. WebContent下新建目录放入jsp,跳转servlet页面出错解决

    为方便分类jsp文件,于是在web-content下新建了一级目录,将jsp文件放入其中,结果原本跳转的servlet出现404错误: 解决如下: 在用eclipse创建的servle会自动生成一个注 ...

  7. C++调用windowsAPI

    1.需要#include <windows.h>http://zhidao.baidu.com/link?url=yOeEGkhe3-kVI6rCqyNp14IjTyXBkQhLeNt-X ...

  8. sql中级语句

    创建联结 select n_title,n_content,t_name,t_memo from nrc_news,nrc_type where nrc_news.t_id=nrc_type.t_id ...

  9. [bzoj3887][Usaco2015 Jan]Grass Cownoisseur_trajan_拓扑排序_拓扑序dp

    [Usaco2015 Jan]Grass Cownoisseur 题目大意:给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在 ...

  10. 使用echo $? 查看命令成功执行的原理

    转载于:http://blog.csdn.net/cmzsteven/article/details/49049387 在进行源代码编译,或者执行命令无法确认所执行的命令是否成功执行的情况下,我们都会 ...