Search code examples
c#nunitresharperconsole-applicationshadow-copy

Get application directory in both console application and its unit tests


I have a console application project with NUnit tests in the same project.

I have been trying to apply this solution.

At run-time the solution worked OK. But when I ran the tests by Resharper test runner or NUnit GUI runner, GetExecutingAssembly().Location returned a path like this: d:\Temp\f4ctjcmr.ofr\nojeuppd.fmf\R2Nbs\assembly\dl3\9766f38e\b9496fb3_43cccf01\.
Disabling shadow-copying fixed the problem in both test runners, but new problems appeared (VS is not able to build the project until NUnit Gui is closed). Is there a better solution than disabling shadow-copying?

Update: Environment.GetCommandLineArgs()[0] returned C:\Program Files (x86)\NUnit 2.6.3\bin\ in the tests running in NUnit Gui with shadow-copying enabled.


Solution

  • The following solution worked for me. Please vote to its author if it helps you.

    As explained in the MSDN forums post, How to convert URI path to normal filepath?, I used the following:

    // Get normal filepath of this assembly's permanent directory
    var path = new Uri(
            System.IO.Path.GetDirectoryName(
            System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
        ).LocalPath;