How to get Current Project Directory path using C#

Once the code is compiled and running, 'Project Path' has no meaning. All you can determine are the file locations of the compiled assemblies. And you can only do what you are asking if your Console project references the built 'class library' DLL directly, rather than via a Project Reference.

Then, you can make use of Reflection to get Assembly paths like;

string path = Assembly.GetAssembly(typeof (SomeClassInOtherProject)).Location;

I believe the problem is:

Since the Console project has the DLL file reference it is using DLL to call any methods. At this time it is returning the class library projct's DLL location which is located in console project's bin directory and it doesn't know about the physical location of class library project.

so essentially it is returning the same project path. I will have to move both projects in same directory in order to solve this issue.


If you loading the class library from another assembly.

string Path = System.Reflection.Assembly.GetAssembly(typeof({LibraryClassName})).Location;

string PathToClassLibPro = Path.GetDirectoryName( Path);

Replace {LibraryClassName} with the class name of your library.

Tags:

C#

.Net