如何使用C#来获得当前项目目录路径如何使用、路径、目录、项目

2023-09-03 00:54:26 作者:寡淡

好了,这是个问题。我有两个项目,一个是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();

Environment.CurrentDirectory; 

这是给我的控制台项目,我使用要调用的方法路径。

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

上述方法给我路径类似

C:\\ConsolePro\\bin\\Debug

但我需要的类库项目的路径

but I need the path of Class library project

C:\\ClassLibPro\\bin\\Debug

请指教

推荐答案

我相信问题是:

由于控制台项目有它使用的DLL调用任何方法的DLL文件的参考。 这时它返回类库projct的DLL的位置,位于控制台项目的bin目录,它不知道类库项目的物理位置。

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.