4

I am really breaking my head for the simple unit testing i am writing!

[TestFixture]
Class A
{

  [TestMethod]
  public void test()
  {
      Assembly.GetExecutingAssembly().Location // gives some temporary local path
  }

 }

All i want is to get the path of the assembly location (where my project folder resides) not some local or temporary folder.

This folder is what I get if I put a breakpoint: \AppData\Local\Temp\

Any help is greatly appreciated!

EDIT: I tried doing this with both nUnit as well as MSTest microsoft's unit testing! Nothing helps.

  • Refer this : http://stackoverflow.com/questions/52797/how-do-i-get-the-path-of-the-assembly-the-code-is-in – Koushik Jul 17 '13 at 19:28
  • actually this is used in the dll which I am testing! But, during the unit testing, this path is the path of the Temp folder. Since i can't change the dll code, I need the path of the assembly location! – now he who must not be named. Jul 17 '13 at 19:34
  • Yes Its does behave in a funny way when used with Nunit. Please read the link that I send you thoroughly. You will find your answer in it. More precisely look at the second answer with 257 up-votes. – Koushik Jul 17 '13 at 19:39
  • @Koushik: thanks for the link. But again, pls note that I can't modify the code of the dll that I am running and this statement is just inside that dll! – now he who must not be named. Jul 17 '13 at 19:45
  • ok. If you want to get the path of the assembly that is being referenced in your application you can use `Path.GetDirectoryName(Assembly.Load("AssemblyName").Location)`. But if you want to get the path of the main assembly that is a wrong way to do it because you are trying to access the assembly path of a different project that is in the same solution. As you know when you make a reference, a copy of the dll is copied to the local directory and all the operations are done on it. So even if you remove the project from where the dll is reference it still works. May be this will help. – Koushik Jul 17 '13 at 23:39

4 Answers4

1

Below code will work.

Path.GetDirectoryName(new System.Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath)
PPr
  • 81
  • 4
0

Did you try

AppDomain.CurrentDomain.BaseDirectory   
Kurubaran
  • 8,696
  • 5
  • 43
  • 65
  • 1
    actually this is used in the dll which I am testing! But, during the unit testing, this path is the path of the Temp folder. Since i can't change the dll code, I need the path of the assembly location! – now he who must not be named. Jul 17 '13 at 19:34
0

For ASP.Net, it doesn't work. I found a better covered solution at Why AppDomain.CurrentDomain.BaseDirectory not contains "bin" in asp.net app?. It works for both Win Application and ASP.Net Web Application.

public string ApplicationPath
{
    get
    {
        if (String.IsNullOrEmpty(AppDomain.CurrentDomain.RelativeSearchPath))
        {
            return AppDomain.CurrentDomain.BaseDirectory; //exe folder for WinForms, Consoles, Windows Services
        }
        else
        {
            return AppDomain.CurrentDomain.RelativeSearchPath; //bin folder for Web Apps 
        }
    }
}
TPG
  • 2,811
  • 1
  • 31
  • 52
0

If GetExecutingAssembly() isn't the executable you're expecting, why not just use a known type in the assembly you want.

typeof(A).Assembly.Location;
Jeremy Lakeman
  • 9,515
  • 25
  • 29