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

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

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

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

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

告诉我,你认为哪一个更好

我希望这个能帮上忙:

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

这将获得项目目录

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

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

当然,你会想要包含这种内部validation/error handling逻辑。

如果您不知道解决scheme所在的目录是什么,则需要执行以下操作:

  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*/} } 

如果您只使用GetCurrrentDirectory()方法,则无论您正在debugging还是释放,都将获得构build文件夹。 我希望这有帮助! 如果你忘记validation,它会是这样的:

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

我也在找这个 我有一个运行HWC的项目,我想保持网站不在应用程序树中,但我不想将它保存在debugging(或发布)目录中。 FWIW,接受的解决scheme(也是这个)只能识别运行可执行文件的目录。

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

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

另一种方法来做到这一点

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

如果你想获得bin文件夹的path

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

也许有更好的办法=)

在我终于完成了关于公共string的第一个回答以获得一个答案之后,我发现你可能从registry中读取一个值来获得所需的结果。 事实certificate,这条路线甚至更短:

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

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

这里是主要的代码:

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

关于这个答案的说明:

我正在使用Visual Studio 2008专业版。 如果您使用的是其他版本(即2003,2005,2010等),那么您可能不得不修改SubKeystring的“版本”部分(即8.0,7.0等)。

如果你使用我的答案之一,如果没有太多要问,那么我想知道你使用我的哪些方法,为什么。 祝你好运。

  • DM

我有一个类似的情况,并且在没有结果的Google之后,我声明了一个公共string,它修改了debugging/发布path的string值来获取项目path。 使用这种方法的一个好处是,因为它使用了currect项目的目录,所以从一个debugging目录或一个release目录开始工作并不重要:

 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; } 

然后,只需要一行,就可以随时调用它:

 string MyProjectDir = DirProject(); 

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

使用它来获得项目目录(为我工作):

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

另一个不完美的解决scheme(但也许比其他一些更完美):

  protected static string GetSolutionFSPath() { return System.IO.Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName; } protected static string GetProjectFSPath() { return String.Format("{0}\\{1}", GetSolutionFSPath(), System.Reflection.Assembly.GetExecutingAssembly().GetName().Name); } 

即使当前项目不是解决scheme的Startup Project ,该版本也会返回当前项目的文件夹。

第一个缺陷是我跳过了所有错误检查。 这可以很容易地修复,但应该只是一个问题,如果您将项目存储在驱动器的根目录中或在path中使用联结(并且联结是解决scheme文件夹的后代),所以此scheme不太可能。 我不完全确定,Visual Studio可以处理这些设置无论如何。

您可能遇到的另一个(更可能)问题是项目名称必须与该项目的文件夹名称匹配才能find。

您可能遇到的另一个问题是该项目必须位于解决scheme文件夹中。 这通常不是问题,但是如果您已经使用“ Add Existing Project to Solution选项将项目添加到解决scheme,那么这可能不是解决scheme的组织方式。

最后,如果应用程序将修改工作目录,则应在此之前存储此值,因为此值是相对于当前工作目录确定的。

当然,这也意味着您不能在项目属性对话框中更改项目的“ Build – >“ Output path或“ Debug – >“ Working directory选项”的默认值。

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

将给你的项目目录。