执行程序集中的相对路径
我正在进行unit testing,测试生成文件的应用程序中的方法。 所以我决定将一个预期的文件放在一个名为Resource / Empower /的目录中。 资源文件夹与Unit Test项目的bin文件夹处于同一级别。
现在我想要做的是获取文件名的路径。 我不能硬编码因为我不确切知道构建服务器上的驱动器。
那么我如何获得文件的相对路径。 让我们说如果文件名是expectedMasterFileSetUp.txt
?
我想要路径Resource/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 ; }
使用Path.GetDirectoryName()对new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath
然后Path.Combine()你的相对路径到结尾。