Learner
Learner

Reputation: 447

How to get Current Project Directory path using C#

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

Upvotes: 14

Views: 76303

Answers (9)

NotNameless
NotNameless

Reputation: 61

I had this exact issue as well where I couldn't access the file in my namespace's bin/debug folder. My solution was to manipulate the string using Split() then construct a new string which is the absolute path to the json file I have in my namespace.

private static string GetFilePath()
        {            
            const char Escape = '\\'; //can't have '\' by itself, it'll throw the "Newline in constant" error
            string directory = Environment.CurrentDirectory;
            string[] pathOccurences = directory.Split(Escape);            
            string pathToReturn = pathOccurences[0] + Escape; //prevents index out of bounds in upcoming loop
            for(int i = 1; i < pathOccurences.Length; i++)
            {
                if (pathOccurences[i] != pathOccurences[i - 1]) //the project file name and the namespace file name are the same
                    pathToReturn += pathOccurences[i] + Escape;
                else
                    pathToReturn += typeof(thisClass).Namespace + Escape; //In the one occurrence of the duplicate substring, I replace it with my class Namespace name
            }
            return pathToReturn + "yourFile.json";
        }

I personally don't like this solution, but it was the only answer I could think of.

Upvotes: 1

John Holliday
John Holliday

Reputation: 1308

I use the following approach to get the current project path at runtime:

public static class ProjectInfo {
   public static string appDirectory = AppDomain.CurrentDomain.BaseDirectory;
   public static string projectPath = appDirectory.Substring(0, appDirectory.IndexOf("\\bin"));
}

Upvotes: 1

Mahdi ghafoorian
Mahdi ghafoorian

Reputation: 1205

Despite i cant find a good solution i use this trick : as long as you want to come back to your ideal path u should add Directory.GetParent() instead of ...

  Directory.GetParent(...(Directory.GetParent(Directory.GetCurrentDirectory()).ToString()...).ToString()

Upvotes: 0

SteMa
SteMa

Reputation: 431

I hope I understand u corretly:

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

Upvotes: 3

RJ Lohan
RJ Lohan

Reputation: 6527

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;

Upvotes: 11

tsells
tsells

Reputation: 2771

I would recommend one of two options.

  1. If the files are small include them in the class library and stream them to a temp location when needed

  2. Other option is to copy the files during the build to the output directory and use them that way. In cases of multiple shared projects it is best to have a common bin folder that you copy assemblies to and run from that location.

Upvotes: 1

Learner
Learner

Reputation: 447

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.

Upvotes: 4

tsukimi
tsukimi

Reputation: 1645

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.

Upvotes: 3

cmastudios
cmastudios

Reputation: 146

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.

Upvotes: 4

Related Questions