c# - 如何打开测试项目代码文件夹中的文件? (Visual Studio 2008 和 .NET C#)

标签 c# .net visual-studio visual-studio-2008 unit-testing

当我在 Visual Studio 上运行测试项目时,我使用下面的代码访问测试项目代码文件夹中的文件

var Location = System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName
);
var FileLocation = Path.Combine(
            Location
            ,@"..\..\..\TestProject1\App_Data\data.xml"
            );

还有另一种(更好的)方法吗?

最佳答案

您是说您正在尝试在运行测试本身的上下文中查找文件?如果是这样,您应该查看 TestContext对象,特别是在 TestDeploymentDir属性(property)。您还应该将文件标记为 DeploymentItem .

关于c# - 如何打开测试项目代码文件夹中的文件? (Visual Studio 2008 和 .NET C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/451499/

相关文章:

c# - 如何处理 DataGridViewCell 中的 KeyEvents?

c# - 事件目录 : get groups where a user is member

c# - 如何以编程方式删除 Azure Active Directory 用户?

c# - 为什么我们不能在 C# 中使用公共(public)字段进行数据绑定(bind)?

c# - VS 2017 版本 15.5.1 - Unresolved System.Net.Http 项目引用

c# - 从存储过程调用C#返回错误代码

c# - $(TargetFrameworkVersion) (v9.0) 小于 Xamarin.Forms (10.0) 所需的最低 $(TargetFrameworkVersion)

c# - 如何确定 DynamoDB 表是否为空?

c# - 我可以制作一个模板,或者在 C# 中生成一堆几乎相同的属性吗?

visual-studio - w3wp 进程只显示 ID 不显示名称