有的时候,当前执行的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. 九十四:CMS系统之cms后台登录限制

    装饰器,验证当前session中是否存在定义的user_id,没有就重定向到登录页 from flask import session, redirect, url_forfrom functools ...

  2. B站动手学深度学习第十八课:seq2seq(编码器和解码器)和注意力机制

    from mxnet import nd h_forward = nd.array([1,2]) h_backward = nd.array([3,4]) h_bi = nd.concat(h_for ...

  3. 安装flanal报错解决

    1.:Error registering network: failed to acquire lease: node "test4" pod cidr not assigned ...

  4. DP经典问题—————(LCIS)最长公共上升子序列

    这道题是LIS(最长上升子序列)与LCS(最长公共子序列)问题的综合版本,有关这两个问题可以看一下我的文章:https://www.cnblogs.com/myhnb/p/11305551.html ...

  5. list-style-type:none是加在ul还是li中呢?

    很多时候我们都需要多对列表元素进行初始化,方法是给列表元素添加list-style-type: none,但作为小白的我是经常纠结一个问题:是把它加在ul中还是li中呢 我试了一下,加在ul和li都能 ...

  6. Sequelize+MySQL存储emoji表情

    一.原因 mysql的utf8编码的一个字符最多3个字节,但是一个emoji表情为4个字节,所以utf8不支持存储emoji表情.但是utf8的超集utf8mb4一个字符最多能有4字节,所以能支持em ...

  7. 上传本地文件到SVN

    前言:今天按照自己的记忆上传本地文件夹到SVN,出现了点问题,重温了简单操作. https://blog.csdn.net/qq_35150366/article/details/81129847 参 ...

  8. PostgreSQL 循环导出schema的脚本

    需要备份的schema列表 $ cat need_backup_schema.txt pipeline_na_16q3_v4 pipeline_na_16q4_v8 pipeline_na_16q4_ ...

  9. [转帖]如何备份及恢复Linux文件权限

    如何备份及恢复Linux文件权限   http://embeddedlinux.org.cn/emb-linux/entry-level/201604/10-5337.html 三年前我就干过 chm ...

  10. python调用hanlp进行命名实体识别

    本文分享自 6丁一的猫 的博客,主要是python调用hanlp进行命名实体识别的方法介绍.以下为分享的全文. 1.python与jdk版本位数一致 2.pip install jpype1(pyth ...