如何使用 C# 获取当前项目目录路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11218776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Current Project Directory path using C#
提问by Learner
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.
好吧,问题来了。我有两个项目,一个是 C# 控制台,另一个是类库。我正在从控制台应用程序访问/调用类库方法。类库项目中有一个名为 Files 的文件夹。
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.
它为我提供了用于调用该方法的 Console 项目的路径。
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
请指教
采纳答案by Learner
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.
由于控制台项目具有 DLL 文件引用,因此它使用 DLL 来调用任何方法。这时候它返回的是类库项目的DLL位置,它位于控制台项目的bin目录下,它不知道类库项目的物理位置。
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.
所以本质上它正在返回相同的项目路径。为了解决这个问题,我必须将两个项目移动到同一目录中。
回答by cmastudios
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.
您应该能够使用Directory.GetParent(Directory.GetCurrentDirectory())几次来获取更高级别的目录,然后将 lib 目录的路径添加到该目录的末尾。
回答by tsukimi
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.
替换{LibraryClassName}为您的库的类名。
回答by RJ Lohan
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.
一旦代码被编译并运行,“项目路径”就没有意义了。您只能确定已编译程序集的文件位置。如果您的控制台项目直接引用构建的“类库”DLL,而不是通过项目引用,那么您只能执行您所要求的操作。
Then, you can make use of Reflection to get Assembly paths like;
然后,您可以使用反射来获取装配路径,例如;
string path = Assembly.GetAssembly(typeof (SomeClassInOtherProject)).Location;
回答by tsells
I would recommend one of two options.
我会推荐两种选择之一。
If the files are small include them in the class library and stream them to a temp location when needed
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.
如果文件很小,请将它们包含在类库中,并在需要时将它们流式传输到临时位置
另一个选项是在构建期间将文件复制到输出目录并以这种方式使用它们。在多个共享项目的情况下,最好有一个公共 bin 文件夹,您可以将程序集复制到该位置并从该位置运行。
回答by SteMa
I hope I understand u corretly:
我希望我能正确理解你:
Path.GetDirectoryName(typeof(Foo.MyFooClass).Assembly.Location);
回答by Mahdi ghafoorian
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.GetParent(Directory.GetCurrentDirectory()).ToString()...).ToString()
回答by John Holliday
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"));
}

