views:

246

answers:

2

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?

+4  A: 

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.

ctacke
"You should also mark the file as a DeploymentItem." How do I do that?
Jader Dias
By adding the [DeploymentItem] attribute to the test. Dod a Google search for it, but a reasonable start is here: http://msdn.microsoft.com/en-us/library/ms182475(VS.80).aspx
ctacke
excellent! it worked!
Jader Dias
+1  A: 

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.

JaredPar
Just for the record: it's not a WebApplication.
Jader Dias
Why the App_data folder then?
JaredPar