在执行程序集相对路径 [英] Relative Path in the executing assembly

查看:92
本文介绍了在执行程序集相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,在这产生一个文件的应用程序测试方法的单元测试。所以,我决定把预期的文件名为资源/赋权/目录。
中的资源文件夹是在同一级别的单元测试项目的bin文件夹。

I am working on Unit tests that test a method in an application that produces a file. So I decided to put an expected file in a directory called Resource/Empower/. The resource folder is at the same level as the bin folder of the Unit Test project.

现在我想要做的就是文件的路径名称。我不能硬编码,因为我并不完全关于构建服务器上的驱动器知道。

Now what I want to do is get the path of the file name. I cannot hard code because I don't know exactly about the drives on the build server.

那么,如何获得文件的相对路径。比方说,如果文件名是 expectedMasterFileSetUp.txt

So how do I get the relative path of the file. Lets say if the file Name is expectedMasterFileSetUp.txt?

我想要的路径资源/Empower/ExpectedMasterFileSetUp.txt

推荐答案

为什么要使用URI?

string AbsolutePathRelativeToEntryPointLocation( string relativePath )
{
  Assembly entryPoint    = Assembly.GetEntryAssembly() ;
  string   basePath      = Path.GetDirectoryName( entryPoint.Location ) ;
  string   combinedPath  = Path.Combine( basePath , relativePath )      ;
  string   canonicalPath = Path.GetFullPath( combinedPath ) ;

  return canonicalPath ;
}

这篇关于在执行程序集相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆