Jader Dias
Jader Dias

Reputation: 90475

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

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?

Upvotes: 1

Views: 1055

Answers (2)

JaredPar
JaredPar

Reputation: 754745

The most reliable way to get ahold of the deploymetn directory for a VSTS unit test is to use the TestContext class. This will be automaticcally hooked up if you define a public property of type and name TestContext on a given TestClass

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. 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.

Upvotes: 2

ctacke
ctacke

Reputation: 67178

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. You should also mark the file as a DeploymentItem.

Upvotes: 5

Related Questions