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

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


当前回答

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

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

string startupPath = Environment.CurrentDirectory;

告诉我,你觉得哪个更好

其他回答

如果你真的想确保你得到源项目目录,不管bin输出路径设置为什么:

添加一个预构建事件命令行(Visual Studio:项目属性->构建事件): echo $(MSBuildProjectDirectory) > $(MSBuildProjectDirectory)\Resources\ProjectDirectory.txt 将ProjectDirectory.txt文件添加到资源。resx项目(如果它还不存在,右键单击项目->添加新项目->资源文件) 使用Resources.ProjectDirectory从代码中访问。

string projPath = Path.GetFullPath(@"..\..\..\");
Console.WriteLine(projPath);

这对我来说一直都很有效。试一试。

我也在找这个。我有一个运行HWC的项目,我想让网站远离应用程序树,但我不想让它在调试(或发布)目录。FWIW是公认的解决方案(这个也是),它只标识可执行文件所在的目录。

找到我一直在用的那个目录

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

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

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

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

$(ProjectDir)
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 bin directory (ie ../bin/)
string projectDirectory = Directory.GetParent(workingDirectory).Parent.FullName;

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