而不是运行其路径硬编码的外部程序,我想获得当前的项目目录。我正在使用自定义任务中的进程调用外部程序。
我该怎么做呢?AppDomain.CurrentDomain.BaseDirectory只是给了我VS 2008的位置。
而不是运行其路径硬编码的外部程序,我想获得当前的项目目录。我正在使用自定义任务中的进程调用外部程序。
我该怎么做呢?AppDomain.CurrentDomain.BaseDirectory只是给了我VS 2008的位置。
当前回答
最好的解决方案
string PjFolder1 =
Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).
Parent.Parent.FullName;
其他解决方案
string pjFolder2 = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)));
测试它,AppDomain.CurrentDomain.BaseDirectory在过去的项目中为我工作,现在我得到调试文件夹....所选的好答案就是不行!
//Project DEBUG folder, but STILL PROJECT FOLDER
string pjDebugFolder = AppDomain.CurrentDomain.BaseDirectory;
//Visual studio folder, NOT PROJECT FOLDER
//This solutions just not work
string vsFolder = Directory.GetCurrentDirectory();
string vsFolder2 = Environment.CurrentDirectory;
string vsFolder3 = Path.GetFullPath(".\\");
//Current PROJECT FOLDER
string ProjectFolder =
//Get Debug Folder object from BaseDirectory ( the same with end slash)
Directory.GetParent(pjDebugFolder).
Parent.//Bin Folder object
Parent. //Project Folder object
FullName;//Project Folder complete path
其他回答
这还将通过从当前执行目录向上导航两级来为您提供项目目录(这不会为每次构建返回项目目录,但这是最常见的)。
System.IO.Path.GetFullPath(@"..\..\")
当然,您希望将其包含在某种验证/错误处理逻辑中。
好吧,2021年,有点晚了…但我在许多项目中发现的所有可能性都让我很恼火:
bin /调试 bin / x86 -调试 bin /调试- net5 0-windows。 ...
来吧……我只需要一行程序(或几乎)来处理测试单元中的一些文件;我需要在所有过去、现在、(可能是未来)的项目中使用它。
因此,如果项目名称与它所在的相对文件夹相同:
使用程序集名称选择项目根文件夹名称; 回去找,直到找到那个名字。
代码示例:
string appName = Assembly.GetExecutingAssembly().GetName().Name;
var dir = new DirectoryInfo(Environment.CurrentDirectory);
while (dir.Name != appName) {
dir = Directory.GetParent(dir.FullName);
}
return dir.FullName;
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;
还有另一个不完美的解决方案(但可能比其他一些更接近完美):
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);
}
这个版本将返回当前项目的文件夹,即使当前项目不是解决方案的启动项目。
第一个缺陷是我跳过了所有的错误检查。这很容易解决,但只有当你将项目存储在驱动器的根目录中或在路径中使用连接(并且该连接是解决方案文件夹的后代)时才会成为问题,所以这种情况不太可能发生。我不完全确定Visual Studio是否能够处理这两种设置。
您可能遇到的另一个(更可能的)问题是,项目名称必须与项目的文件夹名称匹配才能找到它。
您可能遇到的另一个问题是项目必须在解决方案文件夹中。这通常不是问题,但如果您使用“将现有项目添加到解决方案”选项将项目添加到解决方案中,那么这可能不是解决方案的组织方式。
最后,如果您的应用程序将修改工作目录,您应该在修改之前存储这个值,因为这个值是相对于当前工作目录确定的。
当然,这也意味着您不能在项目属性对话框中更改项目的“构建->输出路径”或“调试->工作目录”选项的默认值。
在我最终完成了关于公共字符串的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