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

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


当前回答

获取c#项目根文件夹的正确方法是利用[CallerFilePath]属性获取源文件的完整路径名,然后减去文件名加扩展名,留下项目的路径。

下面是如何做到这一点:

在项目的根文件夹中,添加包含以下内容的ProjectSourcePath.cs文件:

internal static class ProjectSourcePath
{
    private const  string  myRelativePath = nameof(ProjectSourcePath) + ".cs";
    private static string? lazyValue;
    public  static string  Value => lazyValue ??= calculatePath();

    private static string calculatePath()
    {
        string pathName = GetSourceFilePathName();
        Assert( pathName.EndsWith( myRelativePath, StringComparison.Ordinal ) );
        return pathName.Substring( 0, pathName.Length - myRelativePath.Length );
    }
}

字符串?需要一个非常晚的c#版本#nullable enable;如果你没有,那就去掉?。

Assert()函数是我自己的;如果你喜欢过危险的生活,你可以用你自己的代替它,或者省略它。

函数GetSourceFilePathName()的定义如下:

using System.Runtime.CompilerServices

    public static string GetSourceFilePathName( [CallerFilePath] string? callerFilePath = null ) //
        => callerFilePath ?? "";

一旦你有了以上这些,你可以这样使用它:

string projectSourcePath = ProjectSourcePath.Value;

1 'proper'在:万无一失的;可靠;没有假设;不牢靠的:不牢靠的;不一定在某些项目上成功,但在另一些项目上失败;当你改变不相关的东西时,不太可能毫无征兆地坏掉;等。

其他回答

这个解决方案很适合我,在开发和测试和PROD服务器上使用ASP。NET MVC5通过c#:

var projectDir = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);

如果你需要项目目录中的项目配置文件使用:

$(ProjectDir)

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

首先,你必须包括微软。Win32命名空间,这样你就可以使用注册表:

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;等等),那么你可能必须修改子密钥字符串的'version'部分(即8.0,7.0;等等)。

如果你使用了我的答案之一,如果这不是一个过分的要求,那么我想知道你使用了我的方法,为什么。祝你好运。

dm

(因为22个答案是不够的……这里还有一个....)

Mike Nakis给出了一个很好的答案,我在上面添加了一些改进。这只是对他的漂亮代码稍加修饰的版本。

正如Mike指出的,这个类文件必须在项目的根目录中。

下面的内容我没有遇到任何问题,但可能有我没有意识到的细微差别。YMMV。

using System.IO;
using System.Runtime.CompilerServices;

namespace Whatever
{
  internal static class ProjectPathInfo
  {
    public static string CSharpClassFileName = nameof(ProjectPathInfo) + ".cs";
    public static string CSharpClassPath;
    public static string ProjectPath;
    public static string SolutionPath;

    static ProjectPathInfo() {
      CSharpClassPath = GetSourceFilePathName();
      ProjectPath = Directory.GetParent(CSharpClassPath)!.FullName;
      SolutionPath = Directory.GetParent(ProjectPath)!.FullName;
    }

    private static string GetSourceFilePathName( [CallerFilePath] string? callerFilePath = null ) => callerFilePath ?? "";
  }
}

Try:

var pathRegex = new Regex(@"\\bin(\\x86|\\x64)?\\(Debug|Release)$", RegexOptions.Compiled);
var directory = pathRegex.Replace(Directory.GetCurrentDirectory(), String.Empty);

这是不同于其他的解决方案,也考虑到可能的x86或x64构建。

获取c#项目根文件夹的正确方法是利用[CallerFilePath]属性获取源文件的完整路径名,然后减去文件名加扩展名,留下项目的路径。

下面是如何做到这一点:

在项目的根文件夹中,添加包含以下内容的ProjectSourcePath.cs文件:

internal static class ProjectSourcePath
{
    private const  string  myRelativePath = nameof(ProjectSourcePath) + ".cs";
    private static string? lazyValue;
    public  static string  Value => lazyValue ??= calculatePath();

    private static string calculatePath()
    {
        string pathName = GetSourceFilePathName();
        Assert( pathName.EndsWith( myRelativePath, StringComparison.Ordinal ) );
        return pathName.Substring( 0, pathName.Length - myRelativePath.Length );
    }
}

字符串?需要一个非常晚的c#版本#nullable enable;如果你没有,那就去掉?。

Assert()函数是我自己的;如果你喜欢过危险的生活,你可以用你自己的代替它,或者省略它。

函数GetSourceFilePathName()的定义如下:

using System.Runtime.CompilerServices

    public static string GetSourceFilePathName( [CallerFilePath] string? callerFilePath = null ) //
        => callerFilePath ?? "";

一旦你有了以上这些,你可以这样使用它:

string projectSourcePath = ProjectSourcePath.Value;

1 'proper'在:万无一失的;可靠;没有假设;不牢靠的:不牢靠的;不一定在某些项目上成功,但在另一些项目上失败;当你改变不相关的东西时,不太可能毫无征兆地坏掉;等。