开发者

Relative Path in the executing assembly

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.

So how do I get the relative path 开发者_运维问答of the file. Lets say if the file Name is expectedMasterFileSetUp.txt?

I want the path Resource/Empower/ExpectedMasterFileSetUp.txt


Why use a 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 ;
}


Use Path.GetDirectoryName() on the result of new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath then Path.Combine() your relative path onto the end.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜