汤姆熊猫

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

 1         /// <summary>
2 /// 获取Assembly的运行路径
3 /// </summary>
4 /// <returns></returns>
5 private string GetAssemblyPath()
6 {
7 string _CodeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase ;
8
9 _CodeBase = _CodeBase.Substring(8,_CodeBase.Length - 8); // 8是 file:// 的长度
10
11 string[] arrSection = _CodeBase.Split(new char[]{'/'});
12
13 string _FolderPath = "";
14 for(int i=0;i<arrSection.Length-1;i++)
15 {
16 _FolderPath += arrSection[i] + "/";
17 }
18
19 return _FolderPath;
20 }

posted on 2011-10-18 09:44  汤姆熊猫  阅读(930)  评论(0)    收藏  举报