Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Current Project Directory path using C#

Tags:

c#

.net

okay, here is the question. I have two projects one is C# Console and other is Class library. I am accessing/calling Class library method from the console app. There is a folder called Files within the class library project.

I need to get the path of the Class library's files folder but whenever I use

System.IO.Directory.GetCurrentDirectory();

and

Environment.CurrentDirectory; 

it is giving me path of the Console project which I am using to call the method.

Above methods are giving me path like

C:\\ConsolePro\\bin\\Debug

but I need the path of Class library project

C:\\ClassLibPro\\bin\\Debug

Please advise

like image 975
Learner Avatar asked Jun 27 '12 02:06

Learner


1 Answers

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;
like image 200
RJ Lohan Avatar answered Oct 06 '22 00:10

RJ Lohan