首页  >  文章  >  后端开发  >  C# 调用dll获取dll物理路径的方法

C# 调用dll获取dll物理路径的方法

大家讲道理
大家讲道理原创
2016-11-10 14:52:191412浏览

写类库项目时,经常会有某些特殊业务需要用到服务器端的物理路径,使用传统的 System.IO.Directory.GetCurrentDirectory()方法返回的则是WINNT\System32目录,这个一般不能满 足正常的业务需求,而要得到具体运行DLL所在的物理目录可以通过Assembly.GetExecutingAssembly().CodeBase属 性来取得,具体参考方法如下:

/// <summary>  
 /// 获取Assembly的运行路径  
 /// </summary>  
 ///<returns></returns>  
 private string GetAssemblyPath()  
 {  
     string _CodeBase =System.Reflection.Assembly.GetExecutingAssembly().CodeBase ;  
     _CodeBase = _CodeBase.Substring(8,_CodeBase.Length -8);    // 8是file:// 的长度  
     string[] arrSection = _CodeBase.Split(new char[]{&#39;/&#39;});             
     string _FolderPath = "";  
     for(int i=0;i<arrSection.Length-1;i++)  
     {  
         _FolderPath += arrSection[i] + "/";  
     }  
     return _FolderPath;  
 }


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn