简体   繁体   中英

System.Environment.CurrentDirectory returns wrong Directory

I have added a NUnit test project to a project in order to make unit test

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

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.

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\

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

Nunit 有一个名为TestContext.CurrentContext.TestDirectory的属性,自第 3 版发布以来您应该一直使用它。

use

Application.StartupPath

inested of

Environment.CurrentDirectory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM