1

I have two C# projects Proj1 and Proj2, in Visual Studio under a solution. Proj2 makes use of method meth1 in a.cs in Proj1.

In meth1,

public string meth1(){
return Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
}

It returns the full path of Proj2 when I am calling the method from Proj2. Is there a way to let it getting the path of Proj1 regardless where the method is call?

  • 1
    Once your code's compiled, it's completely independent of projects and solutions. Is there a particular reason why you need the path to the project directory? – Simon MᶜKenzie Jan 31 '16 at 08:04
  • [You might want to take a look at this.](http://stackoverflow.com/questions/4764680/how-to-get-the-location-of-the-dll-currently-executing) – Zohar Peled Jan 31 '16 at 08:06
  • There will be no source codes once you deploy your application. You can do this on your machine only, therefore just hardcoding the path is good enough. Alternatively you could parse the sln file, but again this isn't going to work on client machine. – czubehead Jan 31 '16 at 08:20
  • If it's suitable depending on your needs, you can also add this path as a setting in `Proj2`. That way you won't have to re-compile the application if you need to change it in future. – Apostrofix Jan 31 '16 at 13:16

1 Answers1

0

You'll find Proj1.dll's location here: System.Reflection.Assembly.GetAssembly(typeof(Class1)).Location, where Class1 is the type wherein the meth1 is defined. It can be called form outside of the Proj1.dll assembly.

If you are in the Proj1.dll, then you can use: Assembly.GetExecutingAssembly().Location;

romanoza
  • 4,775
  • 3
  • 27
  • 44