gpt4 book ai didi

c# - 执行程序集中的相对路径

转载 作者:行者123 更新时间:2023-11-30 21:15:29 25 4
gpt4 key购买 nike

我正在进行单元测试,测试生成文件的应用程序中的方法。所以我决定把一个预期的文件放在一个名为 Resource/Empower/的目录中。resource文件夹与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 ;
}

关于c# - 执行程序集中的相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5723818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com