How to get Current Project Directory path using C#

66,901

Solution 1

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.

Solution 2

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;

Solution 3

You should be able to use Directory.GetParent(Directory.GetCurrentDirectory()) a few times to get higher level directories and then add the path of the lib directory to the end of that.

Solution 4

I hope I understand u corretly:

Path.GetDirectoryName(typeof(Foo.MyFooClass).Assembly.Location);

Solution 5

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.

Share:
66,901

Related videos on Youtube

Author by

Learner

Updated on July 09, 2022

Comments

  • Learner 6 months

    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

    • tsells
      tsells over 10 years
      FYI you will never get the path of the dll directory as Visual Studio will copy the dependencies from their original location (ClassLibPro) to the bin / debug folder of your project (Console Pro). There is no way to trace this back during run time.
  • Learner over 10 years
    No, this is not the answer. I need to know how I can get the current project's path where my file is located. Please read the question again!
  • Learner over 10 years
    This doesn't work. Path is coming as the DLL which is located with in console project.
  • Learner over 10 years
    How to define the assembly type at the place of {assembly}. I am using like this string Path=System.Reflection.Assembly.GetExecutingAssembly().Locat‌​ion;
  • tsukimi
    tsukimi over 10 years
    Which class name did you use? your library project class? Why didnt it work, wrong path?
  • Learner over 10 years
    Check my answer. I have explained in it. Your code does returns location but the DLL location which is in the Console 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.
  • Sveta Fishka 11 months
    It worked for me, to get a more higher level directories, thanks a lot !

Related