Resharper runs UnitTest from different location

前端 未结 10 776
清歌不尽
清歌不尽 2020-12-01 15:46

When I run unit tests with Visual Studio it works fine, because it runs from project directory where all assemblies are. But when I run it with resharper it

10条回答
  •  被撕碎了的回忆
    2020-12-01 16:09

    Just to complete the very helpful answer from mcdon, using assembly.Location gives the correct answer as per MSFT's explanation:

    The CodeBase is a URL to the place where the file was found, while the Location is the path from where it was actually loaded. For example, if the assembly was downloaded from the internet, its CodeBase may start with “http://”, but its Location may start with “C:\”. If the file was shadow copied, the Location would be the path to the copy of the file in the shadow-copy dir.

    It’s also good to know that the CodeBase is not guaranteed to be set for assemblies in the GAC. Location will always be set for assemblies loaded from disk, however.

    Therefore I would use the following:

    public static DirectoryInfo GetAssemblyDirectory()
    {
        var assembly = Assembly.GetExecutingAssembly();    
        return new DirectoryInfo(Path.GetDirectoryName(assembly.Location));
    }
    

提交回复
热议问题