Reputation: 5355
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
Upvotes: 3
Views: 3655
Reputation: 74227
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 ;
}
Upvotes: 3
Reputation: 13940
Use Path.GetDirectoryName() on the result of
new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath
then Path.Combine() your relative path onto the end.
Upvotes: 1