创建自定义 MSBuild 任务时,如何从 C# 代码中获取当前项目目录?

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

How do you get the current project directory from C# code when creating a custom MSBuild task?

c#msbuild

提问by sean

Instead of running an external program with its path hardcoded, I would like to get the current Project Dir. I'm calling an external program using a process in the custom task.

我想获取当前的项目目录,而不是运行带有硬编码路径的外部程序。我正在使用自定义任务中的进程调用外部程序。

How would I do that? AppDomain.CurrentDomain.BaseDirectory just gives me the location of VS 2008.

我该怎么做?AppDomain.CurrentDomain.BaseDirectory 只是给了我 VS 2008 的位置。

采纳答案by Iralda Mitro

You can try one of this two methods.

您可以尝试这两种方法之一。

string startupPath = System.IO.Directory.GetCurrentDirectory();

string startupPath = Environment.CurrentDirectory;

Tell me, which one seems to you better

告诉我,你觉得哪个更好

回答by brian kitcehner

Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.Parent.FullName

Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.Parent.FullName

Will give you the project directory.

会给你项目目录。

回答by Brett

I was looking for this too. I've got a project that runs HWC, and I'd like to keep the web site out of the app tree, but I don't want to keep it in the debug (or release) directory. FWIW, the accepted solution (and this one as well) only identifies the directory the executable is running in.

我也在找这个。我有一个运行 HWC 的项目,我想将网站保留在应用程序树之外,但我不想将它保留在调试(或发布)目录中。FWIW,公认的解决方案(以及这个解决方案)仅标识可执行文件正在运行的目录。

To find that directory, I've been using

为了找到那个目录,我一直在使用

string startupPath = System.IO.Path.GetFullPath(".\").

回答by digitalmythology

I had a similar situation, and after fruitless Googles, I declared a public string, which mods a string value of the debug / release path to get the project path. A benefit of using this method is that since it uses the currect project's directory, it matters not if you are working from a debug directory or a release directory:

我也遇到过类似的情况,在谷歌无果之后,我声明了一个公共字符串,它修改了调试/发布路径的字符串值以获取项目路径。使用此方法的一个好处是,由于它使用当前项目的目录,因此无论您是在调试目录还是发布目录中工作都无关紧要:

public string DirProject()
{
    string DirDebug = System.IO.Directory.GetCurrentDirectory();
    string DirProject = DirDebug;

    for (int counter_slash = 0; counter_slash < 4; counter_slash++)
    {
        DirProject = DirProject.Substring(0, DirProject.LastIndexOf(@"\"));
    }

    return DirProject;
}

You would then be able to call it whenever you want, using only one line:

然后你就可以随时调用它,只使用一行:

string MyProjectDir = DirProject();

This should work in mostcases.

这应该在大多数情况下工作。

回答by digitalmythology

After I had finally finished polishing my first answer regarding the us of public strings to derive an answer, it dawned on me that you could probably read a value from the registry to get your desired result. As it turns out, that route was even shorter:

在我终于完成了关于公共字符串的我们的第一个答案以获得答案之后,我突然意识到您可能可以从注册表中读取一个值以获得所需的结果。事实证明,这条路线甚至更短:

First, you must include the Microsoft.Win32 namespace so you can work with the registry:

首先,您必须包含 Microsoft.Win32 命名空间,以便您可以使用注册表:

using Microsoft.Win32;    // required for reading and / or writing the registry

Here is the main code:

下面是主要代码:

RegistryKey Projects_Key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio.0", false);
string DirProject = (string)Projects_Key.GetValue(@"DefaultNewProjectLocation");

A note on this answer:

关于这个答案的注释:

I am using Visual Studio 2008 Professional Edition. If you are using another version, (i.e. 2003, 2005, 2010; etc.), then you mayt have to modify the 'version' part of the SubKey string (i.e. 8.0, 7.0; etc.).

我使用的是 Visual Studio 2008 专业版。如果您使用的是其他版本(即 2003、2005、2010 等),那么您可能不必修改 SubKey 字符串的“版本”部分(即 8.0、7.0 等)。

If you use one of my answers, and if it is not too much to ask, then I would like to know which of my methods you used and why. Good luck.

如果您使用我的答案之一,如果问的不是太多,那么我想知道您使用了我的哪种方法以及为什么。祝你好运。

  • dm

回答by mohammed sameeh

using System;
using System.IO;

// This will get the current WORKING directory (i.e. \bin\Debug)
string workingDirectory = Environment.CurrentDirectory;
// or: Directory.GetCurrentDirectory() gives the same result

// This will get the current PROJECT directory
string projectDirectory = Directory.GetParent(workingDirectory).Parent.FullName;

回答by abel406

If you want ot know what is the directory where your solution is located, you need to do this:

如果你想知道你的解决方案所在的目录是什么,你需要这样做:

 var parent = Directory.GetParent(Directory.GetCurrentDirectory()).Parent;
            if (parent != null)
            {
                var directoryInfo = parent.Parent;
                string startDirectory = null;
                if (directoryInfo != null)
                {
                    startDirectory = directoryInfo.FullName;
                }
                if (startDirectory != null)
                { /*Do whatever you want "startDirectory" variable*/}
            }

If you let only with GetCurrrentDirectory()method, you get the build folder no matter if you are debugging or releasing. I hope this help! If you forget about validations it would be like this:

如果只让 withGetCurrrentDirectory()方法,则无论是调试还是发布,都将获得 build 文件夹。我希望这有帮助!如果您忘记了验证,它将是这样的:

var startDirectory = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName;

回答by Tret

Another way to do this

另一种方法来做到这一点

string startupPath = System.IO.Directory.GetParent(@"./").FullName;

If you want to get path to bin folder

如果您想获取 bin 文件夹的路径

string startupPath = System.IO.Directory.GetParent(@"../").FullName;

Maybe there are better way =)

也许有更好的方法 =)

回答by user5219877

Use this to get the Project directory (worked for me):

使用它来获取项目目录(对我有用):

string projectPath = 
    Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;

回答by nh43de

This will also give you the project directory by navigating two levels up from the current executing directory (this won't return the project directory for every build, but this is the most common).

这还将通过从当前执行目录向上导航两个级别来为您提供项目目录(这不会为每个构建返回项目目录,但这是最常见的)。

System.IO.Path.GetFullPath(@"..\..\")

Of course you would want to contain this inside some sort of validation/error handling logic.

当然,您希望将其包含在某种验证/错误处理逻辑中。