我希望获取当前项目目录而不是在代码中直接写入外部程序路径。我通过自定义任务中的进程来调用外部程序。
如何实现呢?使用AppDomain.CurrentDomain.BaseDirectory仅会返回VS 2008的位置。
我希望获取当前项目目录而不是在代码中直接写入外部程序路径。我通过自定义任务中的进程来调用外部程序。
如何实现呢?使用AppDomain.CurrentDomain.BaseDirectory仅会返回VS 2008的位置。
using System;
using System.IO;
// Get the current directory and make it a DirectoryInfo object.
// Do not use Environment.CurrentDirectory, vistual studio
// and visual studio code will return different result:
// Visual studio will return @"projectDir\bin\Release\netcoreapp2.0\", yet
// vs code will return @"projectDir\"
var currentDirectory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
// On windows, the current directory is the compiled binary sits,
// so string like @"bin\Release\netcoreapp2.0\" will follow the project directory.
// Hense, the project directory is the great grand-father of the current directory.
string projectDirectory = currentDirectory.Parent.Parent.Parent.FullName;
Parent
三次就足够了吗?或许再加一次? - Mike Nakisstring projPath = Path.GetFullPath(@"..\..\..\");
Console.WriteLine(projPath);
这对我来说一直很有效。你也可以试试。
我也在寻找这个。我有一个运行HWC的项目,并且我希望将网站保留在应用程序树之外,但我不想将其保存在调试(或发布)目录中。有关参考价值,接受的解决方案(以及这个解决方案)仅识别可执行文件所在的目录。
为了找到该目录,我一直在使用
string startupPath = System.IO.Path.GetFullPath(".\\").
string startupPath = System.IO.Directory.GetParent(@"./").FullName;
如果您想获取 bin 文件夹的路径
string startupPath = System.IO.Directory.GetParent(@"../").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是否能够处理这两种设置中的任何一种。
另一个(更有可能的)问题是,项目名称必须与项目的文件夹名称匹配才能找到它。
你可能遇到的另一个问题是,项目必须在解决方案文件夹内。通常这不是问题,但如果您使用添加现有项目到解决方案
选项将项目添加到解决方案中,则解决方案的组织方式可能不是这种方式。
最后,如果您的应用程序将修改工作目录,则应在执行此操作之前存储此值,因为该值相对于当前工作目录确定。
当然,这也意味着您不能更改项目的默认值生成
->输出路径
或调试
->工作目录
选项在项目属性对话框中。
试试这个,它很简单
HttpContext.Current.Server.MapPath("~/FolderName/");
在我终于完成了有关使用公共字符串来得出答案的第一个答案之后,我意识到你可以从注册表中读取值以获得所需的结果。事实证明,这条路线甚至更短:
首先,您必须包括Microsoft.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");
我使用了以下解决方案来完成工作:
string projectDir =
Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\.."));
好的,虽然有点晚了,但我对许多项目中发现的所有可能性感到非常恼火:
拜托了...我只需要一个一行代码(或几乎是)来处理测试单元中的一些文件; 我需要在过去、现在和(也许是未来的)所有项目中使用它。
所以,如果项目名称与其所在的相对文件夹名称相同:
代码示例:
string appName = Assembly.GetExecutingAssembly().GetName().Name;
var dir = new DirectoryInfo(Environment.CurrentDirectory);
while (dir.Name != appName) {
dir = Directory.GetParent(dir.FullName);
}
return dir.FullName;
尝试:
var pathRegex = new Regex(@"\\bin(\\x86|\\x64)?\\(Debug|Release)$", RegexOptions.Compiled);
var directory = pathRegex.Replace(Directory.GetCurrentDirectory(), String.Empty);
这个解决方案与其他解决方案不同,还考虑了可能的x86或x64构建。