在别的开发语言中有很多方法可以调用出当前运行模块的路径(exe比较简单,这里指dll之类的模块)。

C#中是如何做的呢?比如需要访问该模块同一路径下的配置文件或者其它文件,下面是使用的方法:

        protected string GetAssemblyPath()
        {
            string assemblypath = string.Empty;
            assemblypath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
            //// 8是【http://www.cnblogs.com/moon2bird/admin/file:///】的长度,要去掉
            assemblypath = assemblypath.Substring(8, assemblypath.Length - 8);
            assemblypath = Path.GetDirectoryName(assemblypath);
            return assemblypath;
        }

直接使用这个函数即可。System.Reflection.Assembly.GetExecutingAssembly().CodeBase的返回值开头是http://www.cnblogs.com/moon2bird/admin/file:///,所以要去掉。

相关文章:

  • 2021-08-19
  • 2021-11-21
  • 2021-09-26
  • 2021-12-17
  • 2021-09-12
  • 2021-05-25
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-19
  • 2022-12-23
  • 2021-10-17
  • 2022-12-23
  • 2022-12-23
  • 2022-03-10
  • 2022-12-23
相关资源
相似解决方案