C# 如何获取我的项目路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14549766/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:14:21  来源:igfitidea点击:

How to get my project path?

c#formspath

提问by HuMMeR-SI

Possible Duplicate:
get path for my .exe using c#

可能的重复:
使用 c# 获取我的 .exe 的路径

Hello I have a question: How can I get my root project path? what I mean is the first folder in the project where the solution is. I found that command :

您好,我有一个问题:如何获取我的根项目路径?我的意思是解决方案所在的项目中的第一个文件夹。我发现该命令:

System.IO.Directory.GetCurrentDirectory();

However it gives me a specific path to the release folder: wanted_Path/bin/Release

但是它为我提供了发布文件夹的特定路径:wanted_Path/bin/Release

So is there other code, should I cut it manually or put my files in the Release folder??

那么是否还有其他代码,我应该手动剪切它还是将我的文件放在 Release 文件夹中?

采纳答案by Parimal Raj

You can use

您可以使用

string wanted_path = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));

回答by nsconnector

var requiredPath = Path.GetDirectoryName(Path.GetDirectoryName(
System.IO.Path.GetDirectoryName( 
      System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase )));

回答by takirala

This gives you the root folder:

这为您提供了根文件夹:

System.AppDomain.CurrentDomain.BaseDirectory

System.AppDomain.CurrentDomain.BaseDirectory

You can navigate from here using .. or ./ etc.. , Appending .. takes you to folder where .sln file can be found

您可以使用 .. 或 ./ 等从这里导航,附加 .. 将您带到可以找到 .sln 文件的文件夹

For .NET framework (thanks to Adionocomment)

对于 .NET 框架(感谢Adiono评论)

Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"..\..\"))

For .NET core here is a way to do it (thanks to nopara73comment)

对于 .NET 核心,这是一种方法(感谢nopara73评论)

Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..\..\..\")) ;

回答by CharlesB

Your program has no knowledge of where your VS project is, so see get path for my .exeand go ../..to get your project's path.

您的程序不知道您的 VS 项目在哪里,因此请查看我的 .exe 的获取路径../..获取您的项目路径。