简体   繁体   English

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

[英]How to open a file inside a Test Project code folder? (Visual Studio 2008 and .NET C#)

When I run a Test Project on Visual Studio I use the code below to access a file inside the test project code folder 在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"
            );

There is another (and better) way? 还有另一种(更好的方法)吗?

Are you saying that you're trying to find the file in the context of a running test itself? 您是说要在正在运行的测试本身的上下文中查找文件吗? If so you should look at the TestContext object, specifically at the TestDeploymentDir property. 如果是这样,则应查看TestContext对象,尤其是TestDeploymentDir属性。 You should also mark the file as a DeploymentItem . 您还应该将文件标记为DeploymentItem

The most reliable way to get ahold of the deploymetn directory for a VSTS unit test is to use the TestContext class. 保留VSTS单元测试的deploymetn目录的最可靠方法是使用TestContext类。 This will be automaticcally hooked up if you define a public property of type and name TestContext on a given TestClass 如果您在给定的TestClass上定义类型和名称为TestContext的公共属性,则将自动挂接它

private TestContext _context;
public TestContext TestContext { get { return _context;} set { _context = value } }

The TestContext class has several members which point to the actual deployment of test code. TestContext类具有几个成员,这些成员指向测试代码的实际部署。 I'm unfamiliar with how a web application is deployed but one of the properties on this class should point you to the correct directory. 我不熟悉Web应用程序的部署方式,但是此类的属性之一应将您指向正确的目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 2008 / C#:如何在项目中找到死代码? - Visual Studio 2008 / C# : How to find dead code in a project? 如何在Visual Studio项目C#中引用包含的文件 - How to refer to included file inside a visual studio project c# 如何在Visual Studio 2008中将程序集清单添加到C#.NET类库项目? - How to add assembly manifest to a C# .NET class library project in Visual Studio 2008? 如何使用Visual Studio 2008对C#Web服务进行单元测试 - How to unit test C# Web Service with Visual Studio 2008 我可以将 windows (.bat ) 文件放在 Visual Studio C# 项目的资源文件夹中吗? - Can I put a windows (.bat ) file inside the resources folder of a Visual studio C# project? .NET C#Visual Studio 2008错误 - .Net c# visual studio 2008 error Visual Studio 2008自动化测试项目可以读取诸如app.config之类的配置文件? (C#.NET) - A Visual Studio 2008 automated tests project can read a configuration file like app.config? (C# .NET) Visual Studio 2008中C#项目中的C ++项目引用 - C++ project reference in a C# project in visual studio 2008 如何在开源项目的 Visual Studio/C# 代码中不存储 API 密钥 - How to not store API key in visual studio/c# code in an open source project 如何在Visual Studio中使用C#在解决方案中轻松获取文件夹内文件的路径? - How can I easily get the path of a file inside a folder in my solution in Visual Studio with C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM