How to get Directory while running unit test

后端 未结 14 861
余生分开走
余生分开走 2021-02-02 05:07

Hi when running my unit test I\'m wanting to get the directory my project is running in to retrieve a file.

Say I have a Test project named MyProject. Test I run:

<
14条回答
  •  花落未央
    2021-02-02 05:19

    My approach relies on getting the location of the unit testing assembly and then traversing upwards. In the following snippet the variable folderProjectLevel will give you the path to the Unit test project.

    string pathAssembly = System.Reflection.Assembly.GetExecutingAssembly().Location;
    string folderAssembly = System.IO.Path.GetDirectoryName(pathAssembly);
    if (folderAssembly.EndsWith("\\") == false) {
        folderAssembly = folderAssembly + "\\";
    }
    string folderProjectLevel = System.IO.Path.GetFullPath(folderAssembly + "..\\..\\");
    

提交回复
热议问题