napi15
napi15

Reputation: 2402

System.Environment.CurrentDirectory returns wrong Directory

I have added a NUnit test project to a project in order to make unit tests. My main project is in:

C:\Users\myName\Desktop\0120-project\ProjectName\

Unit test project is in:

C:\Users\myName\Desktop\0120-project\ProjectNameTest\

Now when I'm debugging a unit test:

[Test]
public void ExportationTest()
{
    var evan = System.Environment.CurrentDirectory;
    //...           
}

the returned value of evan is:

C:\Users\myName\AppData\Local\JetBrains\ReSharperPlatformVs12

how is that even possible?! I'm not even launching the project from there. I'm using Visual Studio 2013.

Upvotes: 5

Views: 4780

Answers (4)

TECNO
TECNO

Reputation: 322

use

Application.StartupPath

inested of

Environment.CurrentDirectory

Upvotes: 0

Neil
Neil

Reputation: 11919

Nunit has a property called TestContext.CurrentContext.TestDirectory which you should have been using since version 3 came out.

Upvotes: 2

napi15
napi15

Reputation: 2402

In order to fix the problem :

I have found this solution that fixed my problem :

   [Test]
         public void ExportationTest()
         {
       Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
           var evan= System.Environment.CurrentDirectory;
             //...           
         }

Output :

C:\Users\myName\Desktop\0120-project\ProjectNameTest\

P.S : Will work on Desktop based application, not sure for web based applications like asp.net

Upvotes: 3

JuanR
JuanR

Reputation: 7803

From the documentation:

Environment.CurrentDirectory Property

Gets or sets the fully qualified path of the current working directory.

It looks like you are using ReSharper to launch your tests, which is probably setting the working directory to that location.

Upvotes: 3

Related Questions